How do you save values into a YAML file? [duplicate]

Using ruby-1.9.3 (Approach may not work in older versions).

I'm assuming the file looks like this (adjust code accordingly):

---
content:
    session: 0

and is called /tmp/test.yml

Then the code is just:

require 'yaml' # Built in, no gem required
d = YAML::load_file('/tmp/test.yml') #Load
d['content']['session'] = 2 #Modify
File.open('/tmp/test.yml', 'w') {|f| f.write d.to_yaml } #Store