# Quick singleton question

**URL:** <https://www.ruby-forum.com/t/quick-singleton-question/96030>\
**Category:** Ruby\
**Created:** [June 23, 2006, 6:47am UTC](https://www.ruby-forum.com/t/quick-singleton-question/96030 "2006-06-23T06:47:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 23, 2006, 6:47am UTC](https://www.ruby-forum.com/t/quick-singleton-question/96030/1 "2006-06-23T06:47:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 23, 2006, 7:15am UTC](https://www.ruby-forum.com/t/quick-singleton-question/96030/2 "2006-06-23T07:15:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 23, 2006, 8:04am UTC](https://www.ruby-forum.com/t/quick-singleton-question/96030/3 "2006-06-23T08:04:42Z")

</div>

> attr parent  
> block.call(self, depth)  
> root.do\_something\_in\_the\_tree {|node, depth| puts “#{’  
> '\*depth}#{node}” }
> 
> Thanks,
> 
> David

Hi David,

I was actually fishing for the best way to add the instance\_variable and  
accessor at runtime in case I need it another day, but your code below  
is  
nicer than mine for this particular static class, thanks a lot. I was  
using  
a child array and instance\_variable\_set in add\_child (by default assume  
no  
parent) whereas you’re assuming a parent param to initialize by default,  
which is probably cleaner. I especially like the recursive tree-walk  
that  
takes a block. Thanks. 🙂

Cheers,

ben
