YAML::load help

I have a hash of Structs that I am trying to save and load to a file
using YAML. I am able to save the YAML output to file, but when I use
var=YAML::load(File.open(‘file’))
to load the data back in, var does not look like the original hash.
What am I doing wrong?

~Jay

Jayson W. wrote:

I have a hash of Structs that I am trying to save and load to a file
using YAML. I am able to save the YAML output to file, but when I use
var=YAML::load(File.open(‘file’))
to load the data back in, var does not look like the original hash.
What am I doing wrong?

What’s different between var and the original hash?

El Sábado, 23 de Agosto de 2008, Jayson W.
escribió:> I have a hash of Structs that I am trying to save and load to a file

using YAML. I am able to save the YAML output to file, but when I use
var=YAML::load(File.open(‘file’))
to load the data back in, var does not look like the original hash.
What am I doing wrong?

Difficult to help you if you don’t specify and show which are the
difference.

var=YAML::load(File.open(‘file’))

Try File.read

Phlip wrote:

var=YAML::load(File.open(‘file’))

Try File.read

It should work with File.open, too.

irb(main):004:0> File.open("/tmp/foo", “w”) {|f| YAML.dump({1=>2}, f)}
=> #<File:/tmp/foo (closed)>
irb(main):005:0> puts File.read("/tmp/foo")

1: 2
=> nil
irb(main):006:0> File.open("/tmp/foo") {|f| p YAML.load(f)}
{1=>2}
=> nil

Hi Jayson

Jayson W. wrote:

I have a hash of Structs that I am trying to save and load to a file
using YAML. I am able to save the YAML output to file, but when I use
var=YAML::load(File.open(‘file’))

Do you mean:
YAML::load(File.read(‘file’))

to load the data back in, var does not look like the original hash.
What am I doing wrong?

What do you get when you call to_yaml on your struct (‘ss’ is a struct
instance):
irb(main):013:0> ss.to_yaml
=> “— !ruby/struct:Customer \nname: foo\naddress: addr\n”
irb(main):014:0> YAML.load(ss.to_yaml)
=> #<struct Struct::Customer name=“foo”, address=“addr”>

Hash of structs works similarly.
Are you checking the file before you read it back in?

Regards,
Daniel

Jayson W. wrote:

The .yml file shows that there is a !ruby/struct, but after the load,
I am not seeing it. I also tried File.read with the same results.

~Jay

Looks ok as far as I can reconstruct what you’re doing (using
ruby-1.8.6-p287):

require ‘yaml’

Foo = Struct.new :x, :y

h = {“key” => Foo.new(1,2)}

s = YAML.dump(h)
puts s

h2 = YAML.load(s)
p h
p h2

END

Output:


key: !ruby/struct:Foo
x: 1
y: 2
{“key”=>#}
{“key”=>#}

The difference between the original hash and the hash loaded from the
.yml file is that the original data shows the hash key, with a struct
as the hash value:

Key#<struct #Class:0x2c790dcattrib=‘data’, attrib=‘data’>

The data after the yml load looks like:

KeyYAML::DomainType:0x2c7890c and thats it.

The .yml file shows that there is a !ruby/struct, but after the load,
I am not seeing it. I also tried File.read with the same results.

~Jay

On Sat, Aug 23, 2008 at 6:22 PM, Joel VanderWerf
[email protected] wrote:

Foo = Struct.new :x, :y

h = {“key” => Foo.new(1,2)}

s = YAML.dump(h)
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thanks Joel,
That helped. The problem a result of how I was adding the Struct value
to the hash. I was trying to condence everything into one line like
this

s={‘key’=>Struct.new(:x,:y).new(1,2)}

Looks like YAML could not recognize this as a struct for some reason.
Doing the same thing in the more traditional two steps, solved the
problem.

Thanks all.

Jayson W. wrote:

That helped. The problem a result of how I was adding the Struct value
to the hash. I was trying to condence everything into one line like
this

s={‘key’=>Struct.new(:x,:y).new(1,2)}

Looks like YAML could not recognize this as a struct for some reason.

irb(main):008:0> Struct.new(:x,:y) == Struct.new(:x,:y)
=> false

So, it’s understandable that creating the Struct class inline would lead
to confusion…