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/