# Refering to an object's 'parent'

**URL:** <https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445>\
**Category:** Ruby\
**Created:** [October 5, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445 "2007-10-05T16:36:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [October 5, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445/1 "2007-10-05T16:36:00Z")

</div>

I’d like to check an object’s “parent” to see if it has a certain  
method, so for example, given …

class House(type)  
attr\_accessor :rooms, :type\_of\_house  
@rooms = []  
…  
def type\_of\_house  
type  
end  
end

class Room  
…  
end

my\_room = Room.new

my\_house = House.new

my\_house.rooms.push(my\_room)

… then is it somehow possible to ask …  
my\_room..respond\_to?(type\_of\_house) ?  
(I know ‘House’ is not really the parent of ‘Room’, and so I suspect the  
answer is “no”, but it can’t hurt to ask. Thanks.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [October 5, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445/2 "2007-10-05T16:46:35Z")

</div>

On 10/5/07, Toby R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> my\_house.rooms.push(my\_room)
> 
> … then is it somehow possible to ask …  
> my\_room..respond\_to?(type\_of\_house) ?  
> (I know ‘House’ is not really the parent of ‘Room’, and so I suspect the  
> answer is “no”, but it can’t hurt to ask. Thanks

I don’t believe so the way you have things setup, as there is no  
relation  
where my\_room knows about my\_house since it is simply kept in an array  
variable inside my\_house.

If you inherit Room \< House then you can do something like this:  
my\_room.class.ancestors[1].respond\_to?(type\_of\_house)

But that’s at the class level (House) whereas you want the instance  
(my\_house) level.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [October 5, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445/3 "2007-10-05T16:55:25Z")

</div>

What I suggest is that you add an attribute to Room that holds a  
reference to the house; then you can set that reference either when  
you instantiate the Room or when you push it onto the array …

-Bill  
[http://billonrails.blogspot.com](http://billonrails.blogspot.com)

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [October 5, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445/4 "2007-10-05T17:31:23Z")

</div>

Thanks! (To both you and Wayne)

Bill S. wrote:

> What I suggest is that you add an attribute to Room that holds a  
> reference to the house; then you can set that reference either when  
> you instantiate the Room or when you push it onto the array …
> 
> -Bill  
> [http://billonrails.blogspot.com](http://billonrails.blogspot.com)

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [October 5, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445/5 "2007-10-05T17:59:20Z")

</div>

On Oct 5, 2007, at 8:36 AM, Toby R. wrote:

> …  
> (I know ‘House’ is not really the parent of ‘Room’, and so I  
> suspect the  
> answer is “no”, but it can’t hurt to ask. Thanks.

sure.

cfp:~ \> cat a.rb  
class House  
attr\_accessor :rooms, :type\_of\_house

def initialize  
@rooms = Room::List.new self  
end

def type\_of\_house  
type  
end

class Room  
attr\_accessor :parent

```
 class List < ::Array
   attr_accessor :house
   def initialize house
     @house = house
   end
   def push room
     super
   ensure
     room.parent = house
   end
 end

```

end  
end

my\_room = House::Room.new

my\_house = House.new

my\_house.rooms.push(my\_room)

p my\_room.parent.respond\_to?(:type\_of\_house)

cfp:~ \> ruby a.rb  
true

probably better design to make a room factory for house though:

cfp:~ \> cat a.rb  
class House  
attr\_accessor :rooms, :type\_of\_house

def initialize  
@rooms = []  
end

def type\_of\_house  
type  
end

def new\_room  
rooms.push(Room.new(self)).last  
end

class Room  
attr\_accessor :house

```
 def initialize house
   @house = house
 end

```

end  
end

my\_house = House.new

my\_room = my\_house.new\_room

p my\_room.house.respond\_to?(:type\_of\_house)

cfp:~ \> ruby a.rb  
true

kind regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [October 6, 2007, 2:55am UTC](https://www.ruby-forum.com/t/refering-to-an-objects-parent/116445/6 "2007-10-06T02:55:51Z")

</div>

On 10/5/07, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> probably better design to make a room factory for house though:

^^ I couldn’t agree more with Ara’s advice.
