This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Reading and Writing Configuration Files
CodeExpectedActual
require 'yaml'
configuration = { 'color' => 'blue',
                  'font' => 'Septimus',
                  'font-size'  => 7 }
open('text.cfg', 'w') { |f| YAML.dump(configuration, f) }
open('text.cfg') { |f| puts f.read }
---
font-size: 7
color: blue
font: Septimus
--- 
font-size: 7
color: blue
font: Septimus
open('text.cfg') { |f| YAML.load(f) }
{"font-size"=>7, "color"=>"blue", "font"=>"Septimus"} {"font-size"=>7, "color"=>"blue", "font"=>"Septimus"}
configuration = [ { 'name' => 'Alice', 'donation' => 50 },
                  { 'name' => 'Bob', 'donation' => 15, 'currency' => "EUR" } ]
open('donors.cfg', 'w') { |f| YAML.dump(configuration, f) }
open('donors.cfg') { |f| puts f.read }
---
- name: Alice
donation: 50
- name: Bob
donation: 15
currency: EUR
--- 
- name: Alice
  donation: 50
- name: Bob
  donation: 15
  currency: EUR
puts ({ 'measurements' => 'metric' }.to_yaml)
---
measurements: metric
--- 
measurements: metric
puts ({ :measurements => :metric }.to_yaml)
---
:measurements: :metric
--- 
:measurements: :metric