Plasticx Blog

Capable of being shaped or formed

Save your file after calling YAML::dump

Posted by Mike 01/25/2007 at 10:04PM

Not saving a yaml file after calling YAML::dump cost me about two hours the other day. Here’s an example:

# load
fnm='myyaml.yml'
h = YAML::load_file(fnm) || Hash.new
# add to hash
h[:foo]='bar'
#save# load
fnm=myyaml.yml
h = YAML::load_file(fnm) || Hash.new
# add to hash
h[:foo]=bar
#save

=begin

  1. don’t do it like this …
    f = File.open(fnm, ‘w’)
    YAML::dump(h, f)
    #close writes the file to disk
    f.close
    =end

# file gets closed after block executes
File.open(fnm, w) do |out|
YAML.dump(h, out)
end

Posted in , |

Trackbacks<

Use the following link to trackback from your own site:
http://plasti.cx/trackbacks?article_id=228


Web Statistics