From: ara.t.howard@... Date: 2006-04-12T00:52:01+09:00 Subject: Re: class variables and inheritance (ActiveRecord problem) On Wed, 12 Apr 2006, Andrew Thompson wrote: > Stephen Bannasch wrote: > >> I'd like to be able to create a class variable that will apply to all >> objects made from that class and ALSO create a derived class with a >> different class variable that is used in objects created from the >> derived class WITHOUT affecting the class variable defined in the >> original class. > > Try a class instance variable vs a class variable: > > class C > @somevar = 'x' > class << C > def somevar > @somevar > end > > def somevar=(var) > @somevar=var > end > end > end > > Its not as easily accessible as a class variable (only accessible from > class methods) and you'd have to initialize it to something in derived > classes (otherwise it's nil) but its non-inherited. Not totally what you > want, but maybe it'll help. > > Andrew this is one of the things traits.rb solves: harp:~ > yes | sudo gem install traits.rb > /dev/null 2>&1 harp:~ > cat a.rb require 'traits' class Base class_trait 'a' => 42 end class Derived < Base end class Derived2 < Base a 42.0 end p Base.a p Derived.a p Derived2.a harp:~ > ruby a.rb 42 42 42.0 the inheritence is smart and allows overriding. regards. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama