Problem with YAML:load

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, 2100)
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:innode_import’
from /usr/local/lib/ruby/1.8/yaml.rb:133:in load' from /usr/local/lib/ruby/1.8/yaml.rb:133:inload’
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:innode_import’
from /usr/local/lib/ruby/1.8/yaml.rb:133:in load' from /usr/local/lib/ruby/1.8/yaml.rb:133:inload’
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-

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.

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

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-

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-