YAML serialisation problem

G

Greg McIntyre

Can somebody tell me why the following code snippet fails?

---8<---8<---8<---8<---8<---

class Parent
def initialize
@x = 5
@child = Child.new(self)
end
end

class Child
def initialize(parent)
@parent = parent # <-- try commenting out this line
end
end

require 'pp'
require 'yaml'

parent = Parent.new
parent_yaml = parent.to_yaml
puts parent_yaml
# --- &id001 !ruby/object:parent
# child: !ruby/object:Child
# parent: *id001
# x: 5
parent2 = YAML.load(parent_yaml)
pp parent2

fail if parent.class != parent2.class

---8<---8<---8<---8<---8<---

It seems YAML works fine for serialising the Parent object _unless_ the
child references it, in which case it looks like the &id001 at the root
of the YAML document throws off the attempt to read it back in as a
Parent and for some reason it resorts to reading it in as a Hash.

What am I doing wrong?
 
G

Greg McIntyre

Sorry, I should add:

$ uname -a
Linux mutton 2.4.18-6mdk #1 Fri Mar 15 02:59:08 CET 2002 i686 unknown

$ ruby -v
ruby 1.8.0 (2003-08-04) [i686-linux]

In case that helps. It's a vanilla Ruby 1.8 installation on a Mandrake
Linux 8.2 installation.
 
W

why the lucky stiff

It seems YAML works fine for serialising the Parent object _unless_ the
child references it, in which case it looks like the &id001 at the root
of the YAML document throws off the attempt to read it back in as a
Parent and for some reason it resorts to reading it in as a Hash.

Unfortunately, there is a bug in handling of circular references in the
version of Syck which was shipped with Ruby 1.8.0. I just tested against
Ruby in CVS and your script works fine now. Ruby 1.8.1 should be released in
a month or so. You could also check out Syck from CVS and rebuild a new
module. [http://whytheluckystiff.net/syck/]

Sorry about that.

_why
 
G

Greg McIntyre

why the lucky stiff said:
Unfortunately, there is a bug in handling of circular references in
the version of Syck which was shipped with Ruby 1.8.0. I just tested
against Ruby in CVS and your script works fine now. Ruby 1.8.1 should
be released in a month or so. You could also check out Syck from CVS
and rebuild a new module. [http://whytheluckystiff.net/syck/]

Sorry about that.

That's alright. :) Syck 0.40 from CVS works fine (though an autogen.sh
style script would have saved me a bit of fumbling about getting it
configured from scratch).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,137
Messages
2,570,795
Members
47,342
Latest member
eixataze

Latest Threads

Top