From: Joel VanderWerf Date: 2006-10-17T22:56:13+09:00 Subject: Re: metaprogramming surprise? Artur Merke wrote: > Jan Svitok schrieb: ... >> There's a difference between class variables (@@x) and singleton class >> instance variables (self.class.@x). They are not the same thing. > > OK, but why are they not the same??? A class variable is not an instance variable of any instance, not even a class. It has its own special semantics of sharing that is quite different from the privacy of an instance variable. class A @@y = "A y" end class B < A @@x = "B x" @@y = "B y" end class A @@x = "A x" end class A p [@@x, @@y] # ==> ["A x", "B y"] end class B p [@@x, @@y] # ==> ["B x", "B y"] end Note that the fact that @@x was assigned first in a subclass makes it distinct in A and B, but the fact that @@y was assigned first in the superclass makes it shared. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407