From: Sean O'Halpin Date: 2006-07-17T12:20:24+09:00 Subject: Re: Alternate initializers or alternate class? On 7/16/06, transfire@gmail.com wrote: > > Sean O'Halpin wrote: > > > +1 - having cake and eating it is a thing devoutly to be wished for ;) > > Well, it seemed all well and good but now I find myself having to > create Auto versions of every other subclass of Hash. I have a few, so > that start's to get ugly. hmm.... > > T. > You don't have to write the code yourself! :) def make_auto(*classes) classes.each do |klass| new_class = Class.new klass do def initialize(*args) super(*args){ |h,k| h[k] = self.class.new } end end Object.const_set("Auto#{klass}", new_class) end end class OrderedHash < Hash # do something here end make_auto Hash, OrderedHash ah = AutoHash.new aoh = AutoOrderedHash.new p ah[:a] p aoh[:b] __END__ {} {} Regards, Sean