[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML serialisation problem

Greg McIntyre

10/6/2003 11:20:00 AM

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?
3 Answers

Greg McIntyre

10/6/2003 11:32:00 AM

0

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://pu... ]===

why the lucky stiff

10/6/2003 1:45:00 PM

0

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.
>

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...]

Sorry about that.

_why


Greg McIntyre

10/7/2003 1:10:00 AM

0

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...]
>
> 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).

--
Greg McIntyre ======[ greg@puyo.cjb.net ]===[ http://pu... ]===