From: Robert Klemme Date: 2009-10-12T18:14:17+09:00 Subject: Re: Class Level inheritable attributes - are we there yet? 2009/10/11 dreamcat four : > On Sun, Oct 11, 2009 at 6:55 PM, Robert Klemme > wrote: >> Having said that I am not sure that you actually need a language construct >> when you can do this: >> >> >> class A >>  class <>    def foo >>      @foo || superclass.foo >>    end >>  end >> > > It shouldn't be mandated to re-invent the wheel in any programming > language, even if its fun style / fun to write. Which wheel did I reinvent? > Here is why Robert: > because you would have just put 2 bugs into your code. > > 1) It breaks when you want to set foo = nil in a subclass. > 2) Not having an accessor definition will break other code. > > puts A.instance_variables.inspect > => ["@foo"] > puts B.instance_variables.inspect > => [] > puts C.instance_variables.inspect > => ["@foo"] David has said everything there is to say about this. I modeled the quick hack based on your original example which obviously needed to maintain constant values at class level which would have been better done like this: 11:07:12 desktop$ ruby19 cl.rb A 1 B 1 C 2 11:07:24 desktop$ cat cl.rb class A Foo = 1 end class B < A end class C < A Foo = 2 end [A,B,C].each do |cl| printf "%-2s %p\n", cl, cl::Foo end If any, that would have been the "bug" in my approach. The code was merely meant to demonstrate what can be done with a few lines of code and that accessors (in fact all methods) defined for class instances are inherited to subclasses. Regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/