title: Can Ruby's YAML module be used to embed comments?
body: <p>The to_yaml method produces nice YAML output, but I would like to include comment lines before  some of the elements.  Is there a way to do so?</p>
  
  <p>For example, I would like to produce:</p>
  
  <pre><code># hostname or IP address of client
  client: host4.example.com
  # hostname or IP address of server
  server: 192.168.222.222
  </code></pre>
  
  <p>From something similar to:</p>
  
  <pre><code>{
    :client =&gt; 'host4.example.com',
    :server =&gt; '192.168.222.222',
  }.to_yaml
  </code></pre>
  
  <p>... but am not sure if the YAML module even has a way to accomplish.</p>
  
  <p>UPDATE:  I ended up not using the solution which used regexes to insert the comments, since it required the separation of the data from the comments.  The easiest and most understandable solution for me is:</p>
  
  <pre><code>require 'yaml'
  
  source = &lt;&lt;SOURCE
  # hostname or IP address of client
  client: host4.example.com
  # hostname or IP address of server
  server: 192.168.222.222
  SOURCE
  
  conf = YAML::load(source)
  
  puts source
  </code></pre>
  
  <p>The benefit to me is that nothing is repeated (for example, 'client:' is only specified once), the data and comments are together, the source can be outputted as YAML, and the data structure (available in conf) is available for use.</p>