From: "Víctor Adrián de la Cruz Serrano" Date: 2009-11-11T16:58:21+09:00 Subject: Need help to detect error 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