title: Read and write YAML files without destroying anchors and aliases?
body: <p>I need to open a YAML file with aliases used inside it:</p>
  
  <pre><code>defaults: &amp;defaults
    foo: bar
    zip: button
  
  node:
    &lt;&lt;: *defaults
    foo: other
  </code></pre>
  
  <p>This obviously expands out to an equivalent YAML document of:</p>
  
  <pre><code>defaults:
    foo: bar
    zip: button
  
  node:
    foo: other
    zip: button
  </code></pre>
  
  <p>Which <code>YAML::load</code> reads it as.</p>
  
  <p>I need to set new keys in this YAML document and then write it back out to disk, preserving the original structure as much as possible.</p>
  
  <p>I have looked at <a href="http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML/Store.html">YAML::Store</a>, but this completely destroys the aliases and anchors.</p>
  
  <p>Is there anything available that could something along the lines of:</p>
  
  <pre><code>thing = Thing.load("config.yml")
  thing[:node][:foo] = "yet another"
  </code></pre>
  
  <p>Saving the document back as:</p>
  
  <pre><code>defaults: &amp;defaults
    foo: bar
    zip: button
  
  node:
    &lt;&lt;: *defaults
    foo: yet another
  </code></pre>
  
  <p>?</p>
  
  <p>I opted to use YAML for this due to the fact it handles this aliasing well, but writing YAML that contains aliases appears to be a bit of a bleak-looking playing field in reality.</p>