G
gwtmp01
I generally avoid class variables but at the last meeting of the New
Haven Ruby Brigade a simple question about them led to a long
discussion and irb session that only served to confuse us all more.
I'm hoping someone on the list can shed some light on a couple of
issues that were raised:
from (irb):5
from (irb):5hello
=> nilNameError: uninitialized class variable @@avar in Object
from (irb):16
from (irb):16
It seems like a block evaluated by class_eval should have access to
the class variables. Similarly, it seems like if an instance method
(get_avar in the example) has access to the class variable then the
variable should also be visible via instance_eval.
In both examples above (class_eval and instance_eval) it seems like
the class variable @@avar is being looked up relative to the
top_level object and not relative to the class and instance objects
respectively.
What am I missing?
Gary Wright
Haven Ruby Brigade a simple question about them led to a long
discussion and irb session that only served to confuse us all more.
I'm hoping someone on the list can shed some light on a couple of
issues that were raised:
NameError: uninitialized class variable @@avar in Objectclass A
@@avar = 'hello'
end => "hello"
A.class_variables => ["@@avar"]
A.class_eval { puts @@avar }
from (irb):5
from (irb):5hello
=> nilNameError: uninitialized class variable @@avar in Object
from (irb):16
from (irb):16
It seems like a block evaluated by class_eval should have access to
the class variables. Similarly, it seems like if an instance method
(get_avar in the example) has access to the class variable then the
variable should also be visible via instance_eval.
In both examples above (class_eval and instance_eval) it seems like
the class variable @@avar is being looked up relative to the
top_level object and not relative to the class and instance objects
respectively.
What am I missing?
Gary Wright