Ruby Terminology Question: Implicit Declaration or no declaration at all?

I have a question, because in most serious sources I read, all say the same No variable is ever declared in Ruby, but still I have doubts, because according to what I was taught in my university implicit declaration is when a variable is created via allocation, where you can specify the type of a variable through conventions established in the language.

This contrasts with explicit declaration, when the data type specified in its declaration, like C… or at least that’s what I understood.

So I was taught wrong?, or I misunderstood something?

What they said:

No variable is ever declared in Ruby

What they meant:

There is no keyword defining a line of code as declaring a variable. Variables can just be used, and at worst are nil.

If you have not previously assigned a value to a variable in Ruby, it becomes nil as soon as you retrieve its value.

class Foo
  def bar
    @bar
  end
end

a = Foo.new
puts a.bar

You weren’t taught wrong. They just describe it in a non academic way.

1

Different variables behave differently in Ruby, but none of them need explicit declaration.

The most interesting behavior is for local variables: Ruby allows you to leave off the receiver for a message send to self (self is the implicit receiver), so foo() is the same thing as self.foo(), and it also allows you to leave off the argument list if there are no arguments, so foo.bar is the same thing as foo.bar(); putting the two together, we get that foo is the same thing as self.foo(), i.e. a message send without arguments to the implicit receiver self. It could, however, also be a reference to the local variable foo. This ambiguity is resolved the following way: foo is interpreted as a message send, unless an assignment to foo has been parsed (but not necessarily executed) before, then from that point on it is interpreted as a local variable.

This is the closest thing Ruby has to variable declaration:

foo
# NameError: undefined local variable or method `foo'

defined?(foo)
# => nil

def foo; 23 end

foo
# => 23

defined?(foo)
# => 'method'

methods
# => [:foo, …]

if false then foo = 42 end

foo
# => nil

defined?(foo)
# => 'local-variable'

local_variables
# => [:foo]

Note that the NameError is not raised because the local variable is undefined, but because Ruby doesn’t know what to make of the expression: is it a local variable or a method call?

Instance variables and global variables behave similarly, they evaluate to nil even if they are not defined:

@foo
# => nil

defined?(@foo)
# => nil

@foo = 42

@foo
# => 42

defined?(@foo)
# => 'instance-variable'

instance_variables
# => [:@foo]

$foo
# => nil

defined?($foo)
# => nil

$foo = 42

$foo
# => 42

defined?($foo)
# => 'global-variable'

global_variables
# => [:$foo, …]

Class hierarchy variables and constants behave differently, they raise NameError if they are not defined:

@@foo
# NameError

defined?(@@foo)
# => nil

@@foo = 42

@@foo
# => 42

defined?(@@foo)
# => 'class-variable'

self.class.class_variables
# => [:@@foo]

FOO
# NameError

defined?(FOO)
# => nil

FOO = 42

FOO
# => 42

defined?(FOO)
# => 'constant'

self.class.constants
# => [:FOO]

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật