title: Inserting an entry in Ruby hash at specific position
body: <p>Given a hash with, say, a nested hash in it:</p>
  
  <pre><code>hash = {"some_key" =&gt; "value",
          "nested" =&gt; {"key1" =&gt; "val1", 
                       "key2" =&gt; "val2"}}
  </code></pre>
  
  <p>and a path to a key in String format: </p>
  
  <pre><code>path = "nested.key2"
  </code></pre>
  
  <p>How can I add a new key-value pair <strong>before</strong> the key2 entry?
  So, expected output should be something like this:</p>
  
  <pre><code>hash = {"some_key" =&gt; "value",
          "nested" =&gt; {"key1" =&gt; "val1",
                      "new_key" =&gt; "new_value"},
                      "key2" =&gt; "val2"}}
  </code></pre>
  
  <p><em><strong>EDITED</em></strong></p>
  
  <p>My goal is to add a kind of label before some key in order to dump hash as Yaml text and post process the text to replace added key/value with Yaml comment. AFAIK, there is no other way to add comment before specific key in YAML programmatically.</p>