From: Artur Merke Date: 2006-10-18T00:05:09+09:00 Subject: Re: metaprogramming surprise? Joel VanderWerf schrieb: > 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 thanx, now I understand. I also recovered my analogy class E def show; print "\noutput show= ",@e; end end class F def self.show; print "\noutput F.show= ",@f; end def show print "\noutput @f= ",@f end end obj= E.new class << obj attr_accessor :e end class << F attr_accessor :f end F.f= "should be 'F.@f', and really is" obj.e= "should be @e, and really is" obj.show F.show F.new.show <<<<<<<<<<<<<<<<<<< results in (the now expected) output show= should be @e, and really is output E.show= should be 'F.@f', and really is output @f= nil So one has to distinguish between using a 'class variable' and 'class instance variable': class A @@class_variable @class_instance_variable def initialize @instance_variable end end I hope this is the correct naming for this variables