From: gwtmp01@... Date: 2006-12-22T09:09:14+09:00 Subject: Re: Class Variable Confusion On Dec 21, 2006, at 4:44 PM, dblack@wobblini.net wrote: > Here's another one you'll like: > > @@avar = 1 > class A > @@avar = "hello" > end > puts @@avar # => hello > > A.class_eval { puts @@avar } # => hello Let's see if I understand what is going on: When @@var is set to 1 at the top-level it is associated with the class Object (a feature of the top-level context) and as such @@var then becomes visible to the entire class hierarchy rooted at Object (i.e., all objects). When @@avar is evaluated in the class A block, Ruby finds @@var already defined for the hierarchy (because A is a subclass of Object). So in this example all three occurrences of @@avar are associated with the same class variable. If you reverse the assignments: class A @@avar = "hello" end @@avar = 1 A.class_eval { puts @@avar } # => 1 class A puts @@avar # => hello end There are now two distinct class variables named @@avar, one is associated with Object and another is associated with class A and shadows the one associated with Object. I still don't understand why class_eval/instance_eval don't affect the resolution of class variables in a manner analogous to how they affect the resolution of instance variables. Is this by design or accident? Gary Wright