From: Christoph Rippel Date: 2001-03-13T13:40:06+09:00 Subject: [ruby-talk:12541] Re: class variables "Dave Thomas" wrote in message news:m266hf9jf7.fsf@zip.local.thomases.com... [...] > > A class variable is shared among all objects of a class, and it is also > > accessible to the class methods that we'll describe later. There is only one > > copy of a particular class variable for a given class. Class variable names > > start with two ``at'' signs, such as ``@@count''. Unlike global and instance > > variables, class variables must be initialized before they are used. Often > > this initialization is just a simple assignment in the body of the class > > definition. > > > > It says 'one copy for a given class' not for `class hieararchy'. > > I agree: we don't really say either way in the book. To make it > clearer that Ruby classes inherit class variables, I'll add some > wording to page 214 at the next printing. In general nice it might be nice if your book could point out in a comprehensive fashion the somewhat confusing differences between a -- variable local to an object -- class variable -- variable local to the Class -- variable local to the Meta Class (the visibility rules are particularly confusing IMO) class A attr_accessor :var @@class_var = "class variable" @var_of_A = "var of A" def class_var @@class_var end end a = A.new p a.var # => nil - not initialized yet a.var = "initialized" p a.var # => "initialized" class << A attr_accessor :var_of_A def class_var= (other) @@class_var = other end def class_var @@class_var end @var_of_A = "var of meta A" end class B < A end b = B.new p b.class_var # => "class variable" p B.class_var # => "class variable" B.class_var= "changed" p a.class_var # => "changed" p B.var_of_A # => nil - not initialized B.var_of_A = "var of B" p B.var_of_A # => "var of B" p A.var_of_A # => "var of A" metaA = ObjectSpace._id2ref( class << A id end ) def metaA.var_of_meta @var_of_A end p metaA.var_of_meta # => "var of meta A" Christoph