From: Steve Tuckner Date: 2002-08-08T04:10:34+09:00 Subject: RE: Class hierarchy... for data I have wanted to do what you talked about. Here is my solution. Please ruby experts make it better if possible :-> class Object def Object.parent_field(*names) names.each do |name| module_eval <<-"end;" def #{name} if type.instance_methods.include?("#{name}_") then send("#{name}_") elsif defined?(@#{name}) then @#{name} else @parent.#{name} end end def #{name}=(value) if type.instance_methods.include?("#{name}_=") then send("#{name}_=", value) else @#{name} = value end end end; end end end class A parent_field :x, :y, :z def initialize(parent) @parent = parent end def x_=(value) @x = value * 2 end end a = A.new(nil) b = A.new(a) c = A.new(b) a.x = 1 a.y = 2 a.z = 3 b.y = 11 b.z = 12 c.z = 21 print "#{a.x}, #{a.y}, #{a.z}\n" print "#{b.x}, #{b.y}, #{b.z}\n" print "#{c.x}, #{c.y}, #{c.z}\n" Steve Tuckner -----Original Message----- From: Tom Gilbert [mailto:gilbertt@fs.tomsflat]On Behalf Of Tom Gilbert Sent: Wednesday, August 07, 2002 12:47 PM To: ruby-talk ML Subject: Re: Class hierarchy... for data * Gavin Sinclair (gsinclair@soyabean.com.au) wrote: > > Anyone got anything similar? Or maybe some suggestions? > > Keep going; you're nearly there. Heh. Just hoping I was missing a nicer way to do it - because once I've implemented all the enumerable and hash methods I'll want this thing to have it's going to be a fairly fat class :) Thanks for the feedback. Tom. -- .^. .-------------------------------------------------------. /V\ | Tom Gilbert, London, England | http://linuxbrit.co.uk | /( )\ | Open Source/UNIX consultant | tom@linuxbrit.co.uk | ^^-^^ `-------------------------------------------------------'