Dear Surlykke and all

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

-- 
Posted via http://www.ruby-forum.com/.