# Problem with YAML:load

**URL:** <https://www.ruby-forum.com/t/problem-with-yaml-load/93143>\
**Category:** Ruby\
**Created:** [April 28, 2007, 5:37am UTC](https://www.ruby-forum.com/t/problem-with-yaml-load/93143 "2007-04-28T05:37:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bino](https://www.ruby-forum.com/u/bino)\
**Post date:** [April 28, 2007, 5:37am UTC](https://www.ruby-forum.com/t/problem-with-yaml-load/93143/1 "2007-04-28T05:37:20Z")

</div>

Dear All.

I’m learning to use yaml4r  
A friend of me, give a simple script for starting point, i save it as  
lyaml01

---------Start lyaml01-----  
Nota = Struct.new(:nomor, :items)  
Item = Struct.new(:description, :quantity, :satuan, :harga\_satuan,  
:total)

nota1 = Nota.new(‘nota-123’, [])  
nota1.items \<\< Item.new(‘gula’, 2, ‘kg’, 100, 2_100)  
nota1.items \<\< Item.new(‘kopi’, 3, ‘kg’, 500, 3_500)

# convert to yaml string

yaml\_str = nota1.to\_yaml

puts ‘— save to file —’  
open("/mnt/ramdisk0/yaml\_file.yml", “w”) do |file|  
file.write(yaml\_str)  
puts ‘— close the file —’  
file.close  
end

puts ‘— reading yaml from file —’  
nota = YAML::load(IO.read("/mnt/ramdisk0/yaml\_file.yml"))  
puts ‘---- display it ----’  
puts nota  
puts ‘— display part —’  
puts nota.nomor

---------Stop Lyaml01 -----

Next, I try to run that script :

—Start lyaml01 run-----

[root@kannel blajarruby]# ruby ./lyaml01  
— save to file —  
— close the file —  
— reading yaml from file —  
---- display it ----  
#\<struct Nota nomor=“nota-123”, items=[#\<struct Item description=“gula”,  
quantity=2, satuan=“kg”, harga\_satuan=100, total=200\>, #\<struct Item  
description=“kopi”, quantity=3, satuan=“kg”, harga\_satuan=500,  
total=1500\>]\>  
— display part —  
nota-123

—Stop lyaml01 run-----

hmmm look nice

And here is the result file generated by that script :

----Start /mnt/ramdisk0/yaml\_file.yml ----  
[root@kannel blajarruby]# more /mnt/ramdisk0/yaml\_file.yml  
— !ruby/struct:Nota  
nomor: nota-123  
items:

- !ruby/struct:Item  
description: gula  
quantity: 2  
satuan: kg  
harga\_satuan: 100  
total: 200
- !ruby/struct:Item  
description: kopi  
quantity: 3  
satuan: kg  
harga\_satuan: 500  
total: 1500

----Stop /mnt/ramdisk0/yaml\_file.yml ----

Next i make a copy , just specific on the “YAML::load” part, to try  
re-read the generated file via IRB :

----Start load yaml irb ------

[root@kannel blajarruby]# irb  
irb(main):002:0\> require ‘yaml’  
=\> true  
irb(main):003:0\> nota =  
YAML::load(IO.read("/mnt/ramdisk0/yaml\_file.yml"))  
TypeError: invalid subclass  
from /usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer' from /usr/local/lib/ruby/1.8/yaml.rb:133:in`node\_import’  
from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load' from /usr/local/lib/ruby/1.8/yaml.rb:133:in`load’  
from (irb):3  
irb(main):004:0\>

----Stop load yaml irb ------

Look like there is problem on this part.

I tried to write a more simple script (i name it “lyaml02”), that will  
just re read the generated file :

-----Start lyaml02 file---------  
[root@kannel blajarruby]# more lyaml02  
require ‘yaml’  
nota = YAML::load(IO.read("/mnt/ramdisk0/yaml\_file.yml"))  
----Start lyaml02 file---------

And try to run lyaml02 script :

-----------Start lyaml02 run --------  
[root@kannel blajarruby]# ruby ./lyaml02  
/usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer': invalid subclass (TypeError) from /usr/local/lib/ruby/1.8/yaml.rb:133:in`node\_import’  
from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load' from /usr/local/lib/ruby/1.8/yaml.rb:133:in`load’  
from ./lyaml02:2

-----------Stop lyaml02 run --------

question :

1. Is it caused by instability of YAML::load ? or
2. Could some body point me to something wrong that I did ?

regards  
-bino-

---

<div class="post-metadata">

**Author:** ![bino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bino](https://www.ruby-forum.com/u/bino)\
**Post date:** [April 28, 2007, 11:36am UTC](https://www.ruby-forum.com/t/problem-with-yaml-load/93143/2 "2007-04-28T11:36:01Z")

</div>

LÃ¸rdag den 28. April 2007 skrev Bino Oetomo:

> :total)  
> file.write(yaml\_str)
> 
> — reading yaml from file —  
> hmmm look nice  
> quantity: 2  
> ----Stop /mnt/ramdisk0/yaml\_file.yml ----  
> irb(main):003:0\> nota =
> 
> -----------Stop lyaml02 run --------
> 
> question :
> 
> 1. Is it caused by instability of YAML::load ? or
> 2. Could some body point me to something wrong that I did ?
> 
> regards  
> -bino-

Hi

I’m not an expert on YAML, but I think YAML has to know about the  
classes Nota  
and Item, if it is to load instances of them.

In other words: You should do something like:

require ‘yaml’

Nota = Struct.new(:nomor, :items)  
Item = Struct.new(:description, :quantity, :satuan, :harga\_satuan)

nota1 = YAML::load(File.open(’/mnt/ramdisk0/yaml\_file.yml’))

puts nota1.inspect

when loading the yaml file.

br. Christian S.

---

<div class="post-metadata">

**Author:** ![bino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bino](https://www.ruby-forum.com/u/bino)\
**Post date:** [April 28, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/problem-with-yaml-load/93143/3 "2007-04-28T15:01:18Z")

</div>

On 28.04.2007 05:37, Bino Oetomo wrote:

> ```
> puts '--- close the file ---'
> 
> ```
> 
> ---------Stop Lyaml01 -----  
> ---- display it ----
> 
> satuan: kg
> 
> YAML::load(IO.read("/mnt/ramdisk0/yaml\_file.yml"))  
> Look like there is problem on this part.  
> And try to run lyaml02 script :  
> -----------Stop lyaml02 run --------
> 
> question :
> 
> 1. Is it caused by instability of YAML::load ? or
> 2. Could some body point me to something wrong that I did ?
> 
> regards  
> -bino-

Without looking too deep into your issue: did you make sure that all  
classes mentioned in the YAML file are defined when loading the file?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![bino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bino](https://www.ruby-forum.com/u/bino)\
**Post date:** [April 30, 2007, 11:44am UTC](https://www.ruby-forum.com/t/problem-with-yaml-load/93143/4 "2007-04-30T11:44:22Z")

</div>

Bino Oetomo wrote:

> ----Start----  
> if m.subject == “bntrans”  
> puts ‘—\> Subject is BNTRANS, Proceed’  
> puts ‘–\> Content of m.body’  
> puts m.body #puts the XMPP msg body  
> #Lets try struct for YAML  
> Nota = Struct.new(:nomor, :items)  
> Item = Struct.new(:description, :quantity, :satuan, :harga\_satuan,  
> :total)  
> puts ‘----\> try YAML::load again m.body’  
> Nota1 = YAML::load(IO.read(m.body))  
> puts ‘-----nota----------’  
> end  
> ----Stop-----

OOppps … my stupidity …  
I have to not using YAML::load(IO.read(m.body))  
m.body is a string variable … so no need of IO.read  
I have to use YAML::load(m.body)

Thank for all the enlightment

sincerely  
-bino-

---

<div class="post-metadata">

**Author:** ![bino](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b19c9b/32.png) [@bino](https://www.ruby-forum.com/u/bino)\
**Post date:** [April 30, 2007, 3:50am UTC](https://www.ruby-forum.com/t/problem-with-yaml-load/93143/5 "2007-04-30T03:50:00Z")

</div>

Dear Surlykke and all

Christian S. wrote:

> LÃ¸rdag den 28. April 2007 skrev Bino Oetomo:  
> In other words: You should do something like:
> 
> require ‘yaml’
> 
> Nota = Struct.new(:nomor, :items)  
> Item = Struct.new(:description, :quantity, :satuan, :harga\_satuan)
> 
> nota1 = YAML::load(File.open(’/mnt/ramdisk0/yaml\_file.yml’))
> 
> puts nota1.inspect
> 
> when loading the yaml file.  
> Thankyou for your enlightment,  
> the codes above is work for me when I try to load and parse “real” YAML  
> file.

Actualy my code is to parse YAML contended in a string-variable received  
from other “external” process.

I try to parse YAML that stored in a variable called m.body.  
Actualy this is a body part of received XMPP msg (based on XMPP4R).

Part of my script that will receive XMPP msg is :

----Start----  
if m.subject == “bntrans”  
puts ‘—\> Subject is BNTRANS, Proceed’  
puts ‘–\> Content of m.body’  
puts m.body #puts the XMPP msg body  
#Lets try struct for YAML  
Nota = Struct.new(:nomor, :items)  
Item = Struct.new(:description, :quantity, :satuan, :harga\_satuan,  
:total)  
puts ‘----\> try YAML::load again m.body’  
Nota1 = YAML::load(IO.read(m.body))  
puts ‘-----nota----------’  
end  
----Stop-----

Below is the results, cutted from where it start to process the msg body  
-------Start results : just plain “puts” to msg body----  
–\> Content of m.body  
— !ruby/struct:Nota  
nomor: Nota-xyz  
items:

- !ruby/struct:Item  
description: gula  
quantity: 2  
satuan: kg  
harga\_satuan: 100  
total: 200
- !ruby/struct:Item  
description: kopi  
quantity: 3  
satuan: kg  
harga\_satuan: 500  
total: 1500  
-------Stop results : just plain “puts” to msg body----  
That is the plain content of the msg body. Is it ok in a matter of  
“YAML::load” ?

-----Start YAML::Load agains the m.body-------------  
----\> try YAML::load again m.body  
08:18:52 EXCEPTION:  
Errno::ENOENT  
No such file or directory - — !ruby/struct:Nota  
nomor: Nota-xyz  
items:  
- !ruby/struct:Item  
description: gula  
quantity: 2  
satuan: kg  
harga\_satuan: 100  
total: 200  
- !ruby/struct:Item  
description: kopi  
quantity: 3  
satuan: kg  
harga\_satuan: 500  
total: 1500

```
./trima06:26:in `read'
./trima06:26
/usr/local/lib/ruby/site_ruby/1.8/callbacks.rb:93:in `call'
/usr/local/lib/ruby/site_ruby/1.8/callbacks.rb:93:in `process'
/usr/local/lib/ruby/site_ruby/1.8/callbacks.rb:92:in `each'
/usr/local/lib/ruby/site_ruby/1.8/callbacks.rb:92:in `process'

```

-----Stop YAML::Load agains the m.body-------------

AFAIK, the content of m.body string is exactly the same of  
/mnt/ramdisk0/yaml\_file.yml  
but it didn’t work

Sincerely  
-bino-
