From: Jim Weirich Date: 2002-08-13T20:41:12+09:00 Subject: Re: Sibling rivalry and class variables >>>>> "Kent" == Kent Dahl writes: Philip> What would you guys suggest I do if I want a variable set Philip> in the parent that the child may or may not override? Kent> Depending on what you wish to do with the variable, I'd suggest: [... several suggestions elided ...] Kent> - Use real class instance variables instead of class variables. (Might Kent> be hard to do if you still want to propagate the variables downwards, Kent> since a subclass is a totally different object.) A class instance variable is just a variable that belongs to the class object rather than an instance of the class. You have to do a little work to reference a class instance variable from an object of that class (setting up a class method is sufficient, perhaps there is an easier way). Elaborating on this suggestion, you could do the following ... class Parent @@cv = "Parent Class Variable" @civ = "Parent Class Instance Variable" def show_cv puts @@cv end def show_civ self.type.show_civ end def self.show_civ puts @civ end end class Son < Parent @@cv = "Son Class Variable" @civ = "Son Class Instance Variable" def self.show_civ puts @civ end end class Daughter < Parent @@cv = "Daughter Class Variable" @civ = "Daughter Class Instance Variable" def self.show_civ puts @civ end end puts "Son ..." s = Son.new s.show_cv s.show_civ puts puts "Daughter ..." d = Daughter.new d.show_cv d.show_civ The output is ... Son ... Daughter Class Variable Son Class Instance Variable Daughter ... Daughter Class Variable Daughter Class Instance Variable Notice that the class instance variable is unique to each subclass and is not shared in the class heirarchy like the @@ class variable. -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)