writing on yaml file

K

Kamarulnizam Rahim

Hi guys,

Is it possible for me to write on existing yaml file (i.e add contents
or change contents) using ruby prgramming? Is there any parser available
out there?

Nizam
 
J

Jeremy Bopp

Hi guys,

Is it possible for me to write on existing yaml file (i.e add contents
or change contents) using ruby prgramming? Is there any parser available
out there?

The standard library defines the YAML class which can parse and emit
YAML documents. There are quite a few examples out there. I tried a
couple from here that seemed to work:

http://yaml4r.sourceforge.net/doc/page/examples.htm

Some code to get you started:

require 'yaml'
# Load the file.
yaml = YAML.load_stream(File.open('myfile.yaml'))
# Add a new key-value pair to the root of the first document.
yaml.documents[0]['new_key'] = 'new_value'
File.open('myfile.yaml', 'w') do |file|
file.write(yaml.emit)
end

The above code assumes that you have a valid YAML file in myfile.yaml.
the first document in that file must have key-value pairs at the root.

-Jeremy
 
Q

Quintus

Am 26.01.2011 05:00, schrieb Jeremy Bopp:
Hi guys,

Is it possible for me to write on existing yaml file (i.e add contents
or change contents) using ruby prgramming? Is there any parser available
out there?

The standard library defines the YAML class which can parse and emit
YAML documents. There are quite a few examples out there. I tried a
couple from here that seemed to work:

http://yaml4r.sourceforge.net/doc/page/examples.htm

Some code to get you started:

require 'yaml'
# Load the file.
yaml = YAML.load_stream(File.open('myfile.yaml'))
# Add a new key-value pair to the root of the first document.
yaml.documents[0]['new_key'] = 'new_value'
File.open('myfile.yaml', 'w') do |file|
file.write(yaml.emit)
end

The above code assumes that you have a valid YAML file in myfile.yaml.
the first document in that file must have key-value pairs at the root.

-Jeremy
You can use the YAML module more or less the same way as Marshal,
therefore the following is a bit easier I suppose. It parses the whole
YAML file and converts it into an appropriate data structure (most
likely a hash) with which you can work. Then call YAML.dump with the
modified object and the file where you want to save it in.

-------------------------------------------------
√ quintus@hades => ~
$ cat test.yml
---
a: 5
b: abcdef
√ quintus@hades => ~
$ irb
irb(main):001:0> require "yaml"
=> true
irb(main):002:0> hsh = YAML.load_file("test.yml")
=> {"a"=>5, "b"=>"abcdef"}
irb(main):003:0> hsh["c"] = 12345
=> 12345
irb(main):004:0> File.open("test.yml", "w"){|f| YAML.dump(hsh, f)}
=> #<File:test.yml (closed)>
irb(main):005:0> exit
√ quintus@hades => ~
$ cat test.yml
---
a: 5
b: abcdef
c: 12345
√ quintus@hades => ~
$
 
K

Kamarulnizam Rahim

When i load the following code:

convert_yaml = YAML::load_file('nizam.yaml')
pp
convert_yaml["System"]["Environmental"]["children"][2]["children"]
nizam =
[{"name"=>"nizam",
"type"=>"Objective",
"subtype"=>"None",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Audit",
"args"=>{:content=>"None\n"}},
{"type"=>"ChildListingComponent",
"title"=>"Current Targets for the Audit Objective:"}]}]
File.open("nizam.yaml", "w"){|f| YAML.dump(nizam, f)}

This error appears:

C:/Users/Hekmatyer/Documents/NetBeansProjects/Nizam_BARU/lib/Environmental.rb:167:in
`[]': can't convert String into Integer (TypeError)

What seems to be the problem? Thanks

Nizam
 
K

Kamarulnizam Rahim

I also alter some of the codes:

convert_yaml = YAML::load_file('nizam.yaml')
pp
convert_yaml["System"]["Environmental"]["children"][2]["children"]
convert_yaml["System"]=
[{"name"=>"nizam",
"type"=>"Objective",
"subtype"=>"None",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Audit",
"args"=>{:content=>"None\n"}},
{"type"=>"ChildListingComponent",
"title"=>"Current Targets for the Audit Objective:"}]}]
File.open("nizam.yaml", "w"){|f| YAML.dump(convert_yaml.to_yaml, f)}

But the same error appears.

Nizam
 
J

Jeremy Bopp

I also alter some of the codes:

convert_yaml = YAML::load_file('nizam.yaml')
pp
convert_yaml["System"]["Environmental"]["children"][2]["children"]
convert_yaml["System"]=
[{"name"=>"nizam",
"type"=>"Objective",
"subtype"=>"None",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Audit",
"args"=>{:content=>"None\n"}},
{"type"=>"ChildListingComponent",
"title"=>"Current Targets for the Audit Objective:"}]}]
File.open("nizam.yaml", "w"){|f| YAML.dump(convert_yaml.to_yaml, f)}

In your previous report, you receive this error:

C:/Users/Hekmatyer/Documents/NetBeansProjects/Nizam_BARU/lib/Environmental.rb:167:in
`[]': can't convert String into Integer (TypeError)

Without knowing how line 167 in Environment.rb relates to the code
snippets in question it's hard to tell where the problem might be. My
guess is that the problem lies in the second line (pp convert_yaml[...)
from each snippet.

First of all, try commenting that out and see if your problem goes away.
If it does, then the problem is likely that you're attempting to use a
string to index into an array when you intend to lookup in a hash. In
other words, the content of convert_yaml has a different structure than
you expect in your code. To further debug this, you should replace your
current pp line with the following lines:

convert_yaml["System"]
convert_yaml["System"]["Environmental"]
convert_yaml["System"]["Environmental"]["children"]
convert_yaml["System"]["Environmental"]["children"][2]["children"]

One of the above lines will reproduce the error, and you can use the
line number of the file listed in the error to identify which of the
statements is problematic.

-Jeremy
 
K

Kamarulnizam Rahim

Hi Jeremy,

Thanks for the post. I am able to solve the problem and the error
message disappear. Thank you

Nizam
 

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
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top