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

Steve Litt slitt at troubleshooters.com
Mon Sep 29 13:07:57 EDT 2008


Whoops, my bad, it's Ruby. I intended it to go to Trilug, home of lots of very 
smart people.

SteveT

On Monday 29 September 2008 07:58:04 am you wrote:
> What language is this?  Did you post to the wrong list?  Not that it's
> unwelcome, just we don't get too much programming discussion on here...
> and since you didn't mention the language I'm wondering if you intended
> this to go somewhere else?
>
> --Jeremy
>
> Steve Litt wrote:
> > 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