From: "Víctor Adrián de la Cruz Serrano" Date: 2009-11-12T09:14:23+09:00 Subject: Re: Need help to detect error Or anyone knows where can I contact some YAML guys to help me with this obscure problem? El mié, 11-11-2009 a las 16:58 +0900, Víctor Adrián de la Cruz Serrano escribió: > I don't know what am I doing wrong, can anyone help me? > These two programs look very similar, but they yield different results. > Can anyone confirm this "error"? > > > ================================================================= > # FAILING EXAMPLE > ================================================================= > class Battlefield > attr_accessor :teams > def initialize > @teams = [] > end > def add_team(team) > @teams << team > team.bparent = self > end > end > > class Team > attr_accessor :members, :bparent > def initialize > @members = [] > @bparent = nil > end > def add_member(child) > @members << child > child.team = self > end > end > > class Warrior > attr_accessor :team > def initialize > @team = nil > end > end > > w = Warrior.new > t = Team.new > t.add_member(w) > b = Battlefield.new > b.add_team(t) > b.inspect > #Convert to YAML and back > y = YAML.load(b.to_yaml) > #Now have a look at the "members" array inside a team... it's gone > y.inspect > y.teams.first.members.inspect > > > ================================================================= > # GOOD EXAMPLE > ================================================================= > require 'yaml' > > class Battlefield > attr_accessor :teams > def initialize > @teams = [] > end > def add_team(team) > @teams << team > team.parent = self > end > end > > class Team > attr_accessor :members, :parent > def initialize > @members = [] > @parent = nil > end > def add_member(child) > @members << child > child.team = self > end > end > > class Warrior > attr_accessor :team > def initialize > @team = nil > end > end > > w = Warrior.new > t = Team.new > t.add_member(w) > b = Battlefield.new > b.add_team(t) > b.inspect > #Convert to YAML and back > y = YAML.load(b.to_yaml) > #Now have a look at the "members" array inside a team, it's still there > y.inspect > y.teams.first.members.inspect > > >