Justin Collins
9/29/2008 8:24:00 PM
Steve Litt wrote:
> Hi all,
>
> In Ruby, 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)
>
That line is unnecessary (I tried your code and commented out that line,
worked fine.)
> obj = YAML::load_file("myfile.yaml");
>
> puts obj.customer.city
>
> That prints "Apopka".
>
> A sample program is at the bottom of this email. As long as the classes are
> defined in the program, they can be accessed. But if an object is created via
> YAML::load_file(), I'm not able to access its data members.
>
> Is there any way I can do it without including the class definition in the
> code?
>
Not really. It cannot generate the classes out of thin air and the YAML
files just store the name of the class. But you could put the class
definitions in separate files so they can be 'required' anywhere you
need them.
-Justin