# Writing on yaml file

**URL:** <https://rubytalk.org/t/writing-on-yaml-file/61587>\
**Category:** ruby-talk\
**Created:** [26 January 2011 01:09 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587 "2011-01-26T01:09:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)\
**Post date:** [26 January 2011 01:09 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/1 "2011-01-26T01:09:20Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [26 January 2011 04:00 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/2 "2011-01-26T04:00:19Z")

</div>

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](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|  
&nbsp;&nbsp;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 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?

---

<div class="post-metadata">

**Author:** ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)\
**Post date:** [27 January 2011 01:26 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/3 "2011-01-27T01:26:54Z")

</div>

Thanks for the post!!

Nizam

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)\
**Post date:** [27 January 2011 01:48 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/4 "2011-01-27T01:48:18Z")

</div>

When i load the following code:

convert\_yaml = YAML::load\_file('nizam.yaml')  
&nbsp;&nbsp;&nbsp;&nbsp;pp  
convert\_yaml["System"]["Environmental"]["children"][2]["children"]  
&nbsp;&nbsp;&nbsp;&nbsp;nizam =  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[{"name"=\>"nizam",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"type"=\>"Objective",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"subtype"=\>"None",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"components"=\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[{"type"=\>"ContentBox",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"title"=\>"Audit",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"args"=\>{:content=\>"None\n"}},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"type"=\>"ChildListingComponent",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"title"=\>"Current Targets for the Audit Objective:"}]}]  
&nbsp;&nbsp;&nbsp;&nbsp;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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)\
**Post date:** [27 January 2011 02:03 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/5 "2011-01-27T02:03:14Z")

</div>

I also alter some of the codes:

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

But the same error appears.

Nizam

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Kamarulnizam\_Rahim](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@Kamarulnizam\_Rahim](https://rubytalk.org/u/Kamarulnizam_Rahim)\
**Post date:** [27 January 2011 03:44 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/6 "2011-01-27T03:44:13Z")

</div>

Hi Jeremy,

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

Nizam

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Marvin\_GA\_lker](https://avatars.discourse-cdn.com/v4/letter/m/e95f7d/32.png) [@Marvin\_GA\_lker](https://rubytalk.org/u/Marvin_GA_lker)\
**Post date:** [26 January 2011 13:55 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/7 "2011-01-26T13:55:20Z")

</div>

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.

> **···**
>
> Am 26.01.2011 05:00, schrieb Jeremy Bopp:
> 
> > 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](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|  
> > &nbsp;&nbsp;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
> 
> -------------------------------------------------  
> √ 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 =\> ~  
> $  
> -------------------------------------------------
> 
> Vale,  
> Marvin

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [27 January 2011 03:26 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/8 "2011-01-27T03:26:26Z")

</div>

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 01/26/2011 08:03 PM, Kamarulnizam Rahim wrote:
> 
> > I also alter some of the codes:
> > 
> > convert\_yaml = YAML::load\_file('nizam.yaml')  
> > &nbsp;&nbsp;&nbsp;&nbsp;pp  
> > convert\_yaml["System"]["Environmental"]["children"][2]["children"]  
> > &nbsp;&nbsp;&nbsp;&nbsp;convert\_yaml["System"]=  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[{"name"=\>"nizam",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"type"=\>"Objective",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"subtype"=\>"None",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"components"=\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[{"type"=\>"ContentBox",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"title"=\>"Audit",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"args"=\>{:content=\>"None\n"}},  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{"type"=\>"ChildListingComponent",  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"title"=\>"Current Targets for the Audit Objective:"}]}]  
> > &nbsp;&nbsp;&nbsp;&nbsp;File.open("nizam.yaml", "w"){|f| YAML.dump(convert\_yaml.to\_yaml, f)}

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 October 2012 07:44 UTC](https://rubytalk.org/t/writing-on-yaml-file/61587/9 "2012-10-12T07:44:11Z")

</div>

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:  
&nbsp;&nbsp;attributes:  
&nbsp;&nbsp;&nbsp;&nbsp;project: Project  
&nbsp;&nbsp;&nbsp;&nbsp;audit: Audit

and my destination file contains  
term:  
&nbsp;&nbsp;attributes:  
&nbsp;&nbsp;&nbsp;&nbsp;project: Project

I want to write audit: Audit to my destination file considering its  
hierarchy.

Thanks  
Marjan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
