Refering to an object's 'parent'

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.

On 10/5/07, Toby R. [email protected] 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.

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

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

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/

On 10/5/07, ara.t.howard [email protected] wrote:

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs