Forum: Ruby Problem with Pickaxe and Yaml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F28f75d17a0c236e079c6ebbb8fff2b9?d=identicon&s=25 Mark Haliday (markh)
on 2005-11-21 20:21
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"?

I also tried this for the last line:

puts data["valuable"]

Thanks for any help on this.
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Daniel.Berger (Guest)
on 2005-11-21 20:34
(Received via mailing list)
Mark Haliday wrote:
>         @valuable = valuable
>     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.  There's no "[]" method defined.  Try 'puts obj'.

That line isn't in the example, btw.

Regards,

Dan
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-11-21 20:38
(Received via mailing list)
On 11/21/05, Mark Haliday <markhaliday@yahoo.com> wrote:
>         @valuable = valuable
>     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
=> #<Foo:0xb7d76310>
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)
=> #<Foo:0xb7c253f4>
irb(main):011:0> b.bar
hello
=> nil
4299e35bacef054df40583da2d51edea?d=identicon&s=25 james (Guest)
on 2005-11-21 20:38
(Received via mailing list)
On Nov 21, 2005, at 1:21 PM, Mark Haliday wrote:

>         @valuable = valuable
>         @volatile = volatile
>         @precious = precious
>     end

attr_reader :valuable

> data = YAML.dump(obj)
> obj = YAML.load(data)
>
> puts obj["valuable"]

puts obj.valuable

Hope that helps.

James Edward Gray II
F28f75d17a0c236e079c6ebbb8fff2b9?d=identicon&s=25 Mark Haliday (markh)
on 2005-11-21 20:39
Daniel.Berger wrote:

> No.  There's no "[]" method defined.  Try 'puts obj'.
>
> That line isn't in the example, btw.
>
> Regards,
>
> Dan


But a similar example is used on page 758 (near the bottom of the page).

If I just do a:

puts obj

It puts out both values, when I just want to get the one in "valuable".

Thanks.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 james (Guest)
on 2005-11-21 20:42
(Received via mailing list)
On Nov 21, 2005, at 1:36 PM, James Edward Gray II wrote:

>> class Special
>>         %w{@precious @valuable}
>>
>> puts obj["valuable"]
>
> puts obj.valuable

Oops, ignore that junk and pay attention to the others who actually
read the problem.  ;)

James Edward Gray II
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 jeff.darklight (Guest)
on 2005-11-21 20:42
(Received via mailing list)
Uh ... your code is *NOT* precisely the example. There's nothing in the
original example that does a ["valuable"] call to either the original or
the
reloaded objects.

puts "Before: #{ obj }"
data = Marshal.dump( obj )
obj = Marshal.load( data )
puts "After: #{ obj }"

Which uses the #to_s function to get access to the internal variables.

If you do the same, you should see the output that they show on p417.

If you aren't going to quote the source verbatim, you probably should
expect
the exact same output.

Anyways, hope that helps.

j.

On 11/21/05, Mark Haliday <markhaliday@yahoo.com> wrote:
> def initialize(valuable, volatile, precious)
> "#@valuable #@volatile #@precious"
> "valuable"?
>
--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood
0817571d150afead454f4220007042fe?d=identicon&s=25 desmarm (Guest)
on 2005-11-21 21:02
(Received via mailing list)
Mark Haliday wrote:

>>Dan
>It puts out both values, when I just want to get the one in "valuable".
>
>Thanks.
>
>
When you write "puts obj" you are calling puts with the object 'obj' as
a parameter.  The prints the result of obj.to_s to stdout.

The to_s method for an object typically returns a string.  You can see
that in the example the to_s method is defined as returning a string
containing the values of three instance variables.

Perhaps part of the confusion has to do with the to_yaml_properties
method. The to_yaml_properties method is only a clue indicating which
data you would like included in the yaml-ization of an object and not
what you can expect from dumping the object.

The difference between the example on the bottom of page 758 and your
example is that the object loaded in the pickaxe example is a Hash and
so it responds to the [] method.

I hope that I've helped more than confused.

Matthew
7d3039b18b2d4db92a13937f09446c9c?d=identicon&s=25 rails_lists (Guest)
on 2005-11-21 21:34
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Actually it's working as expected.
obj does not have a [] method. However you could do the following:

class Special
   attr_reader :valuable
end

puts obj.valuable

or

YAML is serializing obj then deserializing it.
The catch is in the first line of data.

'puts data' shows
- --- !ruby/object:Special
precious: world
valuable: hello

That first line tells YAML that it's going to be loading the data
into an instance of 'Special' If you were to remove that top line
then load it

data = "precious: world\nvaluable: hello"
obj = YAML.load data

it would act as a hash and behave like you wanted it to behave.

puts obj['valuable']
hello

On Nov 21, 2005, at 11:21 AM, Mark Haliday wrote:

>         @valuable = valuable
>     end
>
> I also tried this for the last line:
>
> puts data["valuable"]
>
> Thanks for any help on this.
>
> --
> Posted via http://www.ruby-forum.com/.
>

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFDgi6eG9xIoXK+giARAtz1AJ9SJKaBcPvpGyIPTQXJps/qlSW+lQCeKWnm
2EgbOyNHmPdxdyNBKfvm36c=
=H2q7
-----END PGP SIGNATURE-----
F44dd669a5d2117db09e02d4550c4014?d=identicon&s=25 Stefan Schmiedl (Guest)
on 2005-12-11 17:25
(Received via mailing list)
On Tue, 22 Nov 2005 04:21:05 +0900, 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.

In this case: try walking through your code with irb.

>
> obj = Special.new("Hello", "there", "world")
> data = YAML.dump(obj)
> obj = YAML.load(data)
>
> puts obj["valuable"]

puts obj.valuable

> puts data["valuable"]

data is "just a string" ...

s.
This topic is locked and can not be replied to.