From: Gavin Kistner Date: 2007-07-25T04:30:14+09:00 Subject: Re: Bidirectionnal relation dohzya wrote: > Hi everyone, > I have a problem with bidirectional relations : how to implements it ? How about: class Female attr_accessor :mother def initialize( name ) @name = name @daughters = [] end def daughters @daughters.dup.freeze end def mother=( mom ) @mother = mom unless mom.daughters.include?( self ) mom.add_daughter( self ) end end def add_daughter( girl ) @daughters << girl unless @daughters.include?( girl ) girl.mother = self end def inspect "" end end a = Female.new( 'Strami' ) b = Female.new( 'Lisa' ) c = Female.new( 'Imogen' ) b.mother = a p a.daughters.include?( b ) #=> true b.daughters << c #=> Error: can't modify frozen array (TypeError) b.add_daughter c p a.daughters #=> [] p b.daughters #=> [] p c.mother #=> -- Posted via http://www.ruby-forum.com/.