Sorry if the answer is obvious, but I can’t find a satisfactory or more
verbose explanation about this. The only thing I found is from the
Ruby’s User Guide:
“As with globals, instance variables have the nil value until they are
initialized. Instance variables of ruby do not need declaration. This
implies a flexible structure of objects. In fact, each instance variable
is dynamically appended to an object when it is first referenced.”
Could someone explain the above? How is it more flexible? Also, how do
we differentiate between an unitialized global/instance variable and a
variable that has explicitly been set to ‘nil’?
Sorry if the answer is obvious, but I can’t find a satisfactory or more
verbose explanation about this. The only thing I found is from the
Ruby’s User Guide:
“As with globals, instance variables have the nil value until they are
initialized. Instance variables of ruby do not need declaration. This
implies a flexible structure of objects. In fact, each instance variable
is dynamically appended to an object when it is first referenced.”
Could someone explain the above? How is it more flexible?
You don’t need the declaration. That saves typing and also you can add
them at runtime as you like:
If you would need to declare globals then you either need to include a
declaration for each global you need or you had to add code in each place
that would do the declaration on the fly when needed. That’s tedious and
error prone.
Also, how do
we differentiate between an unitialized global/instance variable and a
variable that has explicitly been set to ‘nil’?
Typically you don’t. There are certain situations where you can use
“defined?” but these are rare. AFAIK it’s seldom used. The more usable
option is to use “global_variables”:
Sorry if the answer is obvious, but I can’t find a satisfactory or more
verbose explanation about this. The only thing I found is from the
Ruby’s User Guide:
“As with globals, instance variables have the nil value until they are
initialized. Instance variables of ruby do not need declaration. This
implies a flexible structure of objects. In fact, each instance variable
is dynamically appended to an object when it is first referenced.”
Could someone explain the above? How is it more flexible?
You don’t need the declaration. That saves typing and also you can add
them at runtime as you like:
Oops, my bad English strikes again. The question should have been: “why
don’t $global and @instance need initialization (i.e. they are already
preset to nil)?” In other words, why does it behave like this?
irb(main):001:0> a
NameError: undefined local variable or method `a’ for main:Object
from (irb):1
irb(main):002:0> a=nil; a
=> nil
irb(main):003:0> $a
=> nil
irb(main):004:0> @a
=> nil
… and not like this?
irb(main):001:0> a
NameError: undefined local variable or method a' for main:Object from (irb):1 irb(main):002:0> a=nil; a => nil irb(main):003:0> $a NameError: undefined global variable $a’
from (irb):3
irb(main):004:0> $a=nil; $a
=> nil
irb(main):005:0> @a
NameError: undefined instance variable `@a’
from (irb):5
irb(main):006:0> @a=nil; @a
=> nil
“D” == David Garamond lists@zara.6.isreserved.com writes:
NameError: undefined local variable or method `a’ for main:Object
^^^^^^^^^^^^^^^^^^^^^^^^
The message is important : ruby don’t know if it’s a variable or a method
and can’t initialize it.
I see. I thought Ruby’s intention was to protect someone from making a
stupid mistake of using uninitialized variables (ala Python). But then I
remember Ruby has the -w switch, so I guess Ruby never enforces this and
just gives a warning.
NameError: undefined local variable or method `a’ for main:Object
^^^^^^^^^^^^^^^^^^^^^^^^
The message is important : ruby don’t know if it’s a variable or a
method
and can’t initialize it.
I see. I thought Ruby’s intention was to protect someone from making a
stupid mistake of using uninitialized variables (ala Python). But then I
remember Ruby has the -w switch, so I guess Ruby never enforces this and
just gives a warning.
Btw, what do people think of adding the following system hooks in Ruby:
“D” == David Garamond lists@zara.6.isreserved.com writes:
Btw, what do people think of adding the following system hooks in Ruby:
What are you trying to solve ?
Making Ruby raise NameError when an uninitialized global or instance
variable is accessed (just like when a new local variable is mentioned
without being assigned first).
“D” == David Garamond lists@zara.6.isreserved.com writes:
Btw, what do people think of adding the following system hooks in
Ruby:
What are you trying to solve ?
Making Ruby raise NameError when an uninitialized global or instance
variable is accessed (just like when a new local variable is mentioned
without being assigned first).
Why do you need that? What about lazy init?
class Foo
def bar;@bar||={};end
end
f = Foo.new
f.bar[“hello”] = “world”
… or the common ad hoc init idiom?
class Bar
def add(name)
(@names||=) << name
end
end
Making Ruby raise NameError when an uninitialized global or instance
variable is accessed (just like when a new local variable is mentioned
without being assigned first).
Why do you need that?
Help in detecting typo?
def foo @barbaz = …
… @quux = 1 if @barrbaz != … # typo
end