I'm having a little issue with YAML

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íctor Adrián.

battle_spec.rb (765 Bytes)

battle2_spec.rb (762 Bytes)

It seems that it has something to do with order. If you check
generated YAML, the members are listed alphabetically.
So if you call it @bparent it comes before @members, and after
@members if it's @parents.

If I comment out line "child.team = self" I get proper @members, but
without @team attribute.

Hypothesis is that order of object creation is important here. I
suggest browsing ruby bugtracker and or sources :slight_smile:

···

2009/10/31 Adrián De la Cruz <adrian@lobotuerto.com>:

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.