Problem with Pickaxe and Yaml

M

Mark Haliday

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

Stefan Schmiedl

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

Daniel Berger

Mark said:
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. There's no "[]" method defined. Try 'puts obj'.

That line isn't in the example, btw.

Regards,

Dan
 
G

Gregory Brown

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 =3D valuable
@volatile =3D volatile
@precious =3D precious
end

def to_yaml_properties
%w{@precious @valuable}
end

def to_s
"#@valuable #@volatile #@precious"
end
end

obj =3D Special.new("Hello", "there", "world")
data =3D YAML.dump(obj)
obj =3D 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
=3D> nil
irb(main):006:0> a =3D Foo.new
=3D> #<Foo:0xb7d76310>
irb(main):008:0> require "yaml"
=3D> true
irb(main):009:0> data =3D YAML.dump(a)
=3D> "!ruby/object:Foo {}\n\n"
irb(main):010:0> b =3D YAML.load(data)
=3D> #<Foo:0xb7c253f4>
irb(main):011:0> b.bar
hello
=3D> nil
 
J

James Edward Gray II

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

attr_reader :valuable
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"]

puts obj.valuable

Hope that helps.

James Edward Gray II
 
M

Mark Haliday

Daniel.Berger said:
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.
 
J

James Edward Gray II

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

attr_reader :valuable
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"]

puts obj.valuable

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

James Edward Gray II
 
J

Jeff Wood

------=_Part_39124_5094103.1132602022150
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 th=
e
reloaded objects.

puts "Before: #{ obj }"
data =3D Marshal.dump( obj )
obj =3D 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 expec=
t
the exact same output.

Anyways, hope that helps.

j.

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 =3D valuable
@volatile =3D volatile
@precious =3D precious
end

def to_yaml_properties
%w{@precious @valuable}
end

def to_s
"#@valuable #@volatile #@precious"
end
end

obj =3D Special.new("Hello", "there", "world")
data =3D YAML.dump(obj)
obj =3D 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.


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

------=_Part_39124_5094103.1132602022150--
 
M

Matthew Desmarais

Mark said:
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.
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
 
J

Jeff Smick

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

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.

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

iD8DBQFDgi6eG9xIoXK+giARAtz1AJ9SJKaBcPvpGyIPTQXJps/qlSW+lQCeKWnm
2EgbOyNHmPdxdyNBKfvm36c=
=H2q7
-----END PGP SIGNATURE-----
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top