Hi --

On Wed, 27 Aug 2008, James Coglan wrote:

>>
>> The easiest way I can think of is:
>>
>>  class Page
>>    class << self
>>      attr_accessor :difference_mapping
>>    end
>>    def self.inherited(c)
>>      c.difference_mapping = difference_mapping
>>    end
>>  end
>>
>>  Page.difference_mapping =  { :blah => :blah }
>>
>>  class Next < Page
>>  end
>>
>>  p Next.difference_mapping   # { :blah => :blah }
>
>
>
> Depends whether you only need the definition done once. This technique will
> not cause the subclass to reflect the parent class' value if it is changed,
> e.g.:
>
> Page.difference_mapping = {:foo => :something}
> Next.difference_mapping #=> {:blah => :blah}

True, but the OP described it as: "Is there a way to define a class
level variable that descends down the inheritance tree unless it is
overridden in the same way as this is possible for methods?" In the
method case, you'd have:

   class A
     def x; 1; end
   end

   class B < A
   end

   class C < A
     def x; 2; end
   end

and even if A changed x, C would still have its override in effect. So
the same deal with the attribute might be OK in this case.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
   Advancing with Rails    January 19-22   Fort Lauderdale, FL
See http://www.rubypal.com for details and updates!