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