From: Jeremy Bopp Date: 2011-01-26T13:00:19+09:00 Subject: Re: writing on yaml file 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