[TriLUG] Accessing a YAML loaded object without the object's definition?

Steve Litt slitt at troubleshooters.com
Sun Sep 28 18:42:39 EDT 2008


Hi all,

I have a fairly complex nested object structure that I serialize to a yaml 
file. Then, in another program, I read it into a varaible like this:

obj = YAML::load_file("myfile.yaml");

I can see all objects and names in obj within irb, but I can't access them 
individually. For instance, in the original native that created the yaml, 
there's this:

obj.customer.city

You cannot access that from the object read in with load_file. It creates an 
error condition.

What DOES work is:

obj = Purchase.new(nil, nil)
obj = YAML::load_file("myfile.yaml");

puts obj.customer.city

That prints "Apopka". 

Is there any way I can do it without including the class definition in the 
code?

Thanks

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US




More information about the TriLUG mailing list