From: David Pollak Date: 2006-06-23T14:10:13+09:00 Subject: Re: Quick singleton question ------=_Part_9651_27424642.1151038965858 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Ben, Try: class Dude attr parent attr kids def initialize(parent) @parent = parent @kids = [] parent.add_kid(self) unless parent.nil? end def do_something_in_the_tree(depth = 0, &block) @kids.each {|kid| kid.do_something_in_the_tree(depth + 1, &block)} block.call(self, depth) end protected def add_kid(the_kid) @kids << the_kid end end Kids know their parent. Parents know their kids. I threw in a way to recursively walk the tree... so you can do something like: root.do_something_in_the_tree {|node, depth| puts "#{' '*depth}#{node}" } Thanks, David On 6/22/06, ben@iagu.net wrote: > > Hi, > > In a structure I'm building, I need to be able to add children (of the > same > class) to an object, and have the child object know who its parent is. So > I > can do obj.instance_variable_set(:@parent,parentobj), but that doesn't > create the accessor for me, so I still need to do > > class << child > def parent > @parent > end > end > > That's not really all that onerous, and I can always predefine attr_reader > for :parent in the main class to save the second step, but it lead me to > wondering if there is a 'nice' way to add an instance variable and its > accessor all at once to an object, because that will probably come in > handy > one day. > > Initially I wanted to do > > class << child > def parent > parentobj > end > end > > but obviously that will fail since parentobj is out of scope. > > Anyway, that's it. :) > > ben > > > -- -------- David Pollak's Ruby Playground http://dppruby.com ------=_Part_9651_27424642.1151038965858--