From: Joel VanderWerf Date: 2007-02-09T06:51:00+09:00 Subject: Re: Class-tree variables oinkoink wrote: > There's been a lot of blog traffic lately about class variables vs. > class instance variables. > I'd like to give an example of something in-between: class-tree > variables. These are sort > of like class variables, but they only point downward in the > inheritance tree rather than > up. That is, each class in an inheritance tree can get a variable > which refers to itself and > all its descendents. Class-tree variables are implemented by class > variables, but the > implementation occurs only in the top (root) class and is inherited by > the rest of the tree. ... This is a solution to a slightly different problem (inheritance of hashes of attributes), but it might be interesting anyway: http://redshift.sourceforge.net/superhash/ A little example: require 'superhash' class A class_superhash :options options[:foo] = "A foo" options[:bar] = "A bar" def options; self.class.options; end end class B < A options[:foo] = "B foo" end p A.options p B.options.to_hash p B.new.options.to_hash __END__ output: {:foo=>"A foo", :bar=>"A bar"} {:foo=>"B foo", :bar=>"A bar"} {:foo=>"B foo", :bar=>"A bar"} -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407