From: "ara.t.howard" Date: 2007-10-21T01:42:03+09:00 Subject: Re: Class instance variable idiom On Oct 19, 2007, at 7:27 PM, David A. Black wrote: > > But then they learn about self and instance variables and they stop > expecting it :-) But I'm not sure I'm following exactly what you mean > here -- i.e., what your vision is of how it should behave as opposed > (?) to how it does behave. > it's not a vision - it's production code, gem released for several years: http://codeforpeople.com/lib/ruby/traits/traits-0.9.2/README you can ignore all the features except inheritance. here's an example of how it works: cfp:~ > cat a.rb require 'traits' class C class << self trait 'a' => 42 end end class B < C end class A < B end B.a 42.0 p A.a p B.a p C.a cfp:~ > ruby a.rb 42 42.0 42 since writing traits i've grown to prefer my attributes library, which is < 100 lines of code and allows one to setup 'smart' class variable inheritance in only a few lines of code cfp:~ > cat a.rb require 'attributes' class C class << self attribute('a'){ catch(:value){ (ancestors - [self]).each do |ancestor| break unless self <= ancestor ancestor.module_eval{ throw :value, @a if defined? @a } end default = 42 } } end end class D < C; end class E < D; end class F < E; end p D.a E.a = 42.0 p F.a cfp:~ > ruby a.rb 42 42.0 i think either for of class variable inheritance is preferable to the @@semantics. it's up in the air whether references or copies should be taken though. regards. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama