title: Load and save a YAML file without losing quotes or comments in Ruby
body: <p>I have a requirement to programmatically edit YAML files in Ruby, and I need to also retain the enclosing quote characters unfortunately.  Being able to retain the comments too would be a bonus.</p>
  
  <p>That is to say if I have a file:</p>
  
  <pre><code>---
  foo: 'bar'
  </code></pre>
  
  <p>or</p>
  
  <pre><code>---
  foo: "bar"
  </code></pre>
  
  <p>My script must not write it back as:</p>
  
  <pre><code>---
  foo: bar
  </code></pre>
  
  <p>And as mentioned, ideally I need to preserve comments too.</p>
  
  <p>Without going down the path of just treating the whole file as a stream of text, is there any convenient way to solve one or both of these problems?</p>