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, 3500)
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 :
- Is it caused by instability of YAML::load ? or
- Could some body point me to something wrong that I did ?
regards
-bino-