From: ara.t.howard@... Date: 2006-05-13T02:48:50+09:00 Subject: Re: Inheriting class-based state On Sat, 13 May 2006, Joel VanderWerf wrote: > Well, there are "class variables", written @@my_name. But they can have > some surprising behaviors in current ruby(*), and in future ruby they > will not be shared up and down the inheritance hierarchy (they will only > be shared between a class and its instances). > > An alternative: > > class A > class << self > attr_accessor :__my_name > > def my_name; A.__my_name; end > def my_name=(n); A.__my_name = n; end > end > end > > class B < A > end > > B.my_name = "fred" > > p A.my_name # ==> "fred" one can just use traits for this: harp:~ > gem install traits harp:~ > cat a.rb require 'traits' class A; class_trait 'my_name' => 'fred'; end class B < A; end p A.my_name p B.my_name B.my_name = 'joe' p A.my_name p B.my_name harp:~ > ruby a.rb "fred" "fred" "fred" "joe" voila - best of both worlds - shared __and__ overridable values if required. -a -- be kind whenever possible... it is always possible. - h.h. the 14th dali lama