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
on 2011-01-26 02:09
on 2011-01-26 05:00
On 01/25/2011 07:09 PM, Kamarulnizam Rahim wrote: > 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
on 2011-01-26 14:58
Am 26.01.2011 05:00, schrieb Jeremy Bopp: > > 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 => ~ $
on 2011-01-27 02:48
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
on 2011-01-27 03:03
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
on 2011-01-27 04:27
On 01/26/2011 08:03 PM, Kamarulnizam Rahim wrote: > [{"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
on 2011-01-27 04:44
Hi Jeremy, Thanks for the post. I am able to solve the problem and the error message disappear. Thank you Nizam
on 2012-10-12 09:43
Kamarulnizam Rahim wrote in post #977784: > Hi Jeremy, > > Thanks for the post. I am able to solve the problem and the error > message disappear. Thank you > > Nizam Hi, My question may not be exactly related to this question but quite similar. I am comparing two yaml files. The first one is my source file and the other one is my destination file. The source file contains KVs that do not exist in my destination file. I want those missing KVs to be written to my destination file. However, I do not want to simply add the missing KVs at the end of my destination file. I want to be able to insert them at the right position within hash hierarchy. So say my source file contains: term: attributes: project: Project audit: Audit and my destination file contains term: attributes: project: Project I want to write audit: Audit to my destination file considering its hierarchy. Thanks Marjan
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.