From: Niklas Frykholm Date: 2001-08-12T16:09:37+09:00 Subject: [ruby-talk:19579] Re: @@variable scope On Sun, Aug 12, 2001 at 06:48:03AM +0900, Tobias Reif wrote: > Below is my current understanding of how families of classes share variables. > I'm sure I got some things completely wrong, and would like to get these > spots pointed out. > > Class variables beginning with @@ are shared and scoped among whole families > of classes: parent classes with descendant classes. If a cousin assigns a new > value to a class variable, the value changes for it's cousin, ancestor, and > descendant classes. If all objects are of class Object, then all classes are > descendant classes of class Object, and all objects of all classes share the > farthest/oldest ancestor class Object. Thus all classes are in the same > family and thus @@ variables would be shared among all objects. > > Where am I erring? I think your view is too static. My understanding is that when a class variable is encountered the class hierarchy is searched upward for a class variable with that name. If a match is found the reference is bound to that variable, otherwise a new class variable is created in the current class. You can see the difference in this example. class A def set(x); @@a=x; end def get; @@a; end end class B