YAML::load help

J

Jayson Williams

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
 
J

Joel VanderWerf

Jayson said:
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?
 
I

Iñaki Baz Castillo

El S=E1bado, 23 de Agosto de 2008, Jayson Williams escribi=F3:
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=3DYAML::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 differenc=
e.


=2D-=20
I=F1aki Baz Castillo
 
D

Daniel Bush

Hi Jayson

Jayson said:
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
 
J

Joel VanderWerf

Phlip said:
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
 
J

Jayson Williams

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:0x2c790dc>attrib='data', attrib='data'>

The data after the yml load looks like:

Key<YAML::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
 
J

Joel VanderWerf

Jayson said:
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:0x2c790dc>attrib='data', attrib='data'>

The data after the yml load looks like:

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

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"=>#<struct Foo x=1, y=2>}
{"key"=>#<struct Foo x=1, y=2>}
 
J

Jayson Williams

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

Joel VanderWerf

Jayson said:
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...
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top