problem with YAML:load

B

Bino Oetomo

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-
 
R

Robert Klemme

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top