# Accessing a YAML loaded object without the object's definition?

**URL:** <https://www.ruby-forum.com/t/accessing-a-yaml-loaded-object-without-the-objects-definition/226027>\
**Category:** Ruby\
**Created:** [September 29, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/accessing-a-yaml-loaded-object-without-the-objects-definition/226027 "2008-09-29T20:06:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [September 29, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/accessing-a-yaml-loaded-object-without-the-objects-definition/226027/1 "2008-09-29T20:06:39Z")

</div>

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)  
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?

Thanks

SteveT

Steve L.  
Recession Relief Package

> **[Recession Relief Package](http://www.troubleshooters.com/bookstore/recession_package_a.htm)**
>
> Learn Troubleshooting and Rapid Learning for career enhancement

############ SAMPLE PROGRAM ###################  
#!/usr/bin/ruby -w

require “yaml”

class Address  
attr\_accessor :street, :city, :state, :zip  
def initialize(street, city, state, zip)  
@street = street  
@city = city  
@state = state  
@zip=zip  
end  
end

class Customer  
attr\_accessor :fname, :lname, :address  
def initialize( fname, lname, address)  
@fname = fname  
@lname = lname  
@address = address  
end  
end

class Item  
attr\_accessor :quantity, :priceper, :name  
def initialize(name, quantity, priceper)  
@name = name  
@quantity = quantity  
@priceper = priceper  
end  
end

class Purchase  
attr\_accessor :customer  
attr\_reader :amount  
attr\_accessor :date  
attr\_reader :items

def initialize(customer, date)  
@customer = customer  
@amount = amount  
@date = date  
@items = Array.new()  
end  
def additem(item)  
@items.push(item)  
end  
def update\_amount()  
@amount = 0  
@items.each do  
|item|  
@amount += (item.quantity.to\_f \* item.priceper.to\_f)  
end  
end  
end

#### MAIN ROUTINE

#### Load objects

address = Address.new([“121 Main Street”, “Suite  
201”], “Apopka”, “FL”, “33333”);

customer = Customer.new(“Steve”, “Litt”, address)

purchase = Purchase.new(customer, “3/3/2033”)  
purchase.additem(Item.new(“Widget”, 1, 1.00));  
purchase.additem(Item.new(“Gadget”, 2, 2.00));  
purchase.additem(Item.new(“Left handed smoke bender”, 3, 3.00));  
purchase.update\_amount()

#### Write out YAML to junk.yaml

ouf = File.new(“./junk.yaml”, “w”);  
ouf.write(YAML::dump(purchase))  
ouf.close()

#### WRITE REPORT

#### Read YAML file and load into Purchase object p

p=Purchase.new(nil, nil)  
p=YAML::load\_file(“./junk.yaml”);

#### Date and total

printf “Purchase on %s, total cost $%0.2f\n”, p.date, p.amount

#### Customer data

printf “\nCUSTOMER DATA:\n”  
printf “%s %s\n”, p.customer.fname, p.customer.lname  
p.customer.address.street.each {|i| puts i}  
printf “%s, %s, %s\n”, p.customer.address.city,  
p.customer.address.state,  
p.customer.address.zip

#### Items

printf “\nITEMS PURCHASED\n”  
printf “%30s %10s %10s %10s\n”, “ITEM NAME”, “QUANTITY”, “@”, “TOTAL”  
p.items.each do  
|item|  
printf “%30s %10d %10.2f %10.2f\n”, item.name, item.quantity,  
item.priceper,  
item.quantity \* item.priceper

end  
printf “%30s %10s %10s %10s\n”, " ", " ", " ", “----------”  
printf “%30s %10s %10s %10.2f\n”, “TOTAL”, " ", " ", p.amount

############ END OF SAMPLE PROGRAM ###################

---

<div class="post-metadata">

**Author:** ![spiz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@spiz](https://www.ruby-forum.com/u/spiz)\
**Post date:** [September 29, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/accessing-a-yaml-loaded-object-without-the-objects-definition/226027/2 "2008-09-29T22:25:26Z")

</div>

Steve L. wrote:

> 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.)

> 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
