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?
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.
···
–
Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://puyo.cjb.net ]===
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
···
On Monday 06 October 2003 05:41 am, Greg McIntyre wrote:
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.
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).
···
why the lucky stiff ruby-talk@whytheluckystiff.net wrote:
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.
–
Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://puyo.cjb.net ]===