From: Frederick Cheung Date: 2008-08-27T19:08:32+09:00 Subject: Re: Difficult Inheritance Problem On 27 Aug 2008, at 10:08, Toby Clemson wrote: > > I would now like to inherit from the Page class but use its > difference_mapping. Obviously this does not work because the > attr_accessor in the DataObject uses a class instance variable that is > unique to each class. I tried using a proper class variable > (@@difference_mapping) but that is the same for all descending > classes. > > 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? I mean I could wrap the data structure in a > method but that does not seem like a very clean solution. > two possible approaches are rails' class_inheritable_accessor and superclass_delegating_accessor. Both lean on class instance variables. In the case of class_inheritable_accessor the attributes are store in a hash and self.inherited is hooked to copy that hash over when a subclass is created superclass_delegating_accessor looks for an appropriate instance variable and if it does not exist calls the superclass (stopping when it gets to the the class that created the superclass_delegating_accessor). Writing however always writes to an instance variable of the class being changed. I put some more detail about them at http://www.spacevatican.org/2008/8/19/fun-with-class-variables Fred > Thanks in advance, > Toby >