Showing posts with label ruby yaml yaml4r. Show all posts
Showing posts with label ruby yaml yaml4r. Show all posts

Sunday, October 29, 2006

ruby and YAML

this tutorial walk through processing yaml files with ruby, I will not go explain why, which, or what is yaml4r but I will explain how to load, save, update yaml file using ruby (Yaml4r).

first of all you need to include yaml.rb in your class by adding this: require 'yaml.rb'

In the following we add an array object to yaml file

Step one: loading object from yaml file.
obj = YAML::load(File.open('test.yaml'))

Step two: modifying the object (which is an array) and save it back to the file.
obj << 'anything' File.open('test.yaml', 'w+') { |out| YAML::dump(obj, out) }



A complete code that do so would look like the following: require 'yaml.rb'

obj = YAML::load(File.open('test.yaml'))

if obj
obj << 'anything'
else
obj = ['anything']
end

File.open('test.yaml', 'w+') { |out|
YAML::dump(obj, out)
}