markh
November 21, 2005, 8:21pm
1
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.
markh
November 21, 2005, 8:34pm
2
Mark H. 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
markh
November 21, 2005, 8:38pm
3
On 11/21/05, Mark H. [email protected] 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
markh
November 21, 2005, 8:38pm
4
On Nov 21, 2005, at 1:21 PM, Mark H. 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 G. II
markh
November 21, 2005, 8:39pm
5
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.
markh
November 21, 2005, 8:42pm
6
On Nov 21, 2005, at 1:36 PM, James Edward G. 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 G. II
markh
November 21, 2005, 8:42pm
7
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 H. [email protected] wrote:
def initialize(valuable, volatile, precious)
“#@valuable #@volatile #@precious ”
“valuable”?
–
“Remember. Understand. Believe. Yield! → http://ruby-lang.org ”
Jeff W.
markh
November 21, 2005, 9:34pm
8
-----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 H. 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-----
markh
November 21, 2005, 9:02pm
9
Mark H. 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
markh
December 11, 2005, 5:25pm
10
On Tue, 22 Nov 2005 04:21:05 +0900, Mark H. 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.