From: Gregory Brown Date: 2005-11-22T04:36:28+09:00 Subject: Re: Problem with Pickaxe and Yaml On 11/21/05, Mark Haliday wrote: > I've got the Pickaxe book (2nd edition) and was doing some of the Yaml > code on page 417. I cannot seem to get the expected results. > > Example: > > require 'yaml' > > class Special > > def initialize(valuable, volatile, precious) > @valuable = valuable > @volatile = volatile > @precious = precious > end > > def to_yaml_properties > %w{@precious @valuable} > end > > def to_s > "#@valuable #@volatile #@precious" > end > end > > obj = Special.new("Hello", "there", "world") > data = YAML.dump(obj) > obj = YAML.load(data) > > puts obj["valuable"] > > The last line throws an error, shouldn't it print out the value for > "valuable"? no... obj becomes an instance of class Special, and acts just like the object before you dumped it. greg@oracle ~ $ irb irb(main):001:0> class Foo irb(main):002:1> def bar irb(main):003:2> puts "hello" irb(main):004:2> end irb(main):005:1> end => nil irb(main):006:0> a = Foo.new => # irb(main):008:0> require "yaml" => true irb(main):009:0> data = YAML.dump(a) => "!ruby/object:Foo {}\n\n" irb(main):010:0> b = YAML.load(data) => # irb(main):011:0> b.bar hello => nil