title: Preserve key order loading YAML from a file in Ruby
body: <p>I want to preserve the order of the keys in a YAML file loaded from disk, processed in some way and written back to disk.</p>
  
  <p>Here is a basic example of loading YAML in Ruby (v1.8.7):</p>
  
  
  
  <pre class="lang-ruby prettyprint-override"><code>require 'yaml'
  
  configuration = nil
  File.open('configuration.yaml', 'r') do |file|
    configuration = YAML::load(file)
    # at this point configuration is a hash with keys in an undefined order
  end
  
  # process configuration in some way
  
  File.open('output.yaml', 'w+') do |file|
    YAML::dump(configuration, file)
  end
  </code></pre>
  
  <p>Unfortunately, this will destroy the order of the keys in <code>configuration.yaml</code> once the hash is built. I cannot find a way of controlling what data structure is used by <code>YAML::load()</code>, e.g. alib's <code>orderedmap</code>.</p>
  
  <p>I've had no luck searching the web for a solution.</p>