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

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