ruby - How do I update Rails locale YAML file without loosing comments and variables? -


i'm building ruby script changes contents of config/locales/*.yml rails locales files. these files contain many useful comments , variables.

by loading, updating, , dumping them, loose these comments , variables.

how programatically update yaml file while preserving comments , variables?

i don't think can.

yaml ignores comments in data file, doesn't parse them, thrown away file loaded. once file loaded they're gone.

the way want can think of, open file outside of yaml, write comments, write yaml content created using to_yaml. like:

require 'yaml'  data = {   'foo' => 'bar', }  file.open('data.yaml', 'w') |fo|   fo.puts "# don't mess this."   fo.puts data.to_yaml end 

which creates:

# don't mess this. --- foo: bar 

Comments