[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML::load help

Jayson Williams

8/23/2008 8:04:00 PM

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

9 Answers

Joel VanderWerf

8/23/2008 9:15:00 PM

0

Jayson Williams 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?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Iñaki Baz Castillo

8/23/2008 9:16:00 PM

0

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

Daniel Bush

8/23/2008 9:25:00 PM

0

Hi Jayson

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

--
Posted via http://www.ruby-....

Phlip

8/23/2008 9:26:00 PM

0

> var=YAML::load(File.open('file'))

Try File.read


Joel VanderWerf

8/23/2008 9:31:00 PM

0

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jayson Williams

8/23/2008 10:04:00 PM

0

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

Joel VanderWerf

8/23/2008 10:22:00 PM

0

Jayson Williams wrote:
> 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>}

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Jayson Williams

8/23/2008 10:46:00 PM

0

On Sat, Aug 23, 2008 at 6:22 PM, Joel VanderWerf
<vjoel@path.berkeley.edu> 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.

Joel VanderWerf

8/24/2008 12:25:00 AM

0

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407