From: "ara.t.howard" Date: 2007-10-06T00:58:33+09:00 Subject: Re: Refering to an object's 'parent' On Oct 5, 2007, at 8:36 AM, Toby Rodwell wrote: > 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. 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/ -- share your knowledge. it's a way to achieve immortality. h.h. the 14th dalai lama