[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Refering to an object's 'parent'

Toby Rodwell

10/5/2007 2:36:00 PM

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.<PARENT>.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.
--
Posted via http://www.ruby-....

3 Answers

Bill Siggelkow

10/5/2007 2:55:00 PM

0

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.bl...

On Oct 5, 2007, at 10:36 AM, Toby Rodwell wrote:

> 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.<PARENT>.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.
> --
> Posted via http://www.ruby-....
>


Toby Rodwell

10/5/2007 3:31:00 PM

0

Thanks! (To both you and Wayne)

Bill Siggelkow 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.bl...

--
Posted via http://www.ruby-....

ara.t.howard

10/5/2007 3:59:00 PM

0


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.<PARENT>.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://codeforp...
--
share your knowledge. it's a way to achieve immortality.
h.h. the 14th dalai lama