From: "Adrián De la Cruz" Date: 2009-10-31T14:11:39+09:00 Subject: I'm having a little issue with YAML --=-WMK83zw2PEocE/09sPMy Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi guys, I'm seeing some strange behavior with YAML serialization/deserialization and would like someone to explain what's going on with this, if possible. I attached two files that you can run with: spec battle_spec.rb (this one fails) spec battle2_spec.rb (this one doesn't fail) The difference between the two, is just a character in the name of an instance variable. The source code is very simple. It has three classes to represent a nested structure: A Battlefield might contain many teams, a Team might contain many Warriors. In my sample it's just one of each, to keep it simple. The problem is that, the Team @members are nullified in some cases. battle_spec.rb shows a case where Team#members is nil. battle2_spec.rb shows a case where Team#members is not nil (just changing a character in a instance variable name, the one that makes the Battlefield accessible for a Team [a var to point to its parent]). I'm kinda new to this testing stuff, maybe I'm doing something wrong, if that's the case, I'll be glad to hear any advice or pointers. If you need more info, I'll be glad to provide it. Best regards, V=C3=ADctor Adri=C3=A1n. --=-WMK83zw2PEocE/09sPMy Content-Disposition: attachment; filename="battle_spec.rb" Content-Type: application/x-ruby; name="battle_spec.rb" Content-Transfer-Encoding: 7bit # -*- coding: utf-8 -*- require 'rubygems' require 'spec' 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 describe Battlefield do describe Team do it "should have members" do w = Warrior.new t = Team.new t.add_member(w) b = Battlefield.new b.add_team(t) y = YAML.load(b.to_yaml) y.teams.first.members.should_not be_nil end end end --=-WMK83zw2PEocE/09sPMy Content-Disposition: attachment; filename="battle2_spec.rb" Content-Type: application/x-ruby; name="battle2_spec.rb" Content-Transfer-Encoding: 7bit # -*- coding: utf-8 -*- require 'rubygems' require 'spec' 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 describe Battlefield do describe Team do it "should have members" do w = Warrior.new t = Team.new t.add_member(w) b = Battlefield.new b.add_team(t) y = YAML.load(b.to_yaml) y.teams.first.members.should_not be_nil end end end --=-WMK83zw2PEocE/09sPMy--