Working with Hashes and YAML in Ruby

Tagged with Ruby YAML

Language: Ruby

View as text

# assuming this yaml file in the same folder...
# 
# first:
#   name: foo
# next:
#   name: hello world


require 'rubygems'
require 'yaml'
root_path = File.dirname(__FILE__)
c = YAML::load(File.open("#{root_path}/config.yml"))
puts c.inspect
puts c["first"]["name"]
puts c["next"]["name"]

# write this out to a new file
File.open ("new.yaml", "w") do |f|
  f  << c.to_yaml
end
Last updated at 13:53 PM on Aug 08, 2008 by Brian Hogan

SnippetStash costs money to host and develop. The service is free for everyone to use
but if you found it useful please consider making a small donation.