title: Iterate over a deeply nested level of hashes in Ruby
body: <p>So I have a hash, and for each level of the hash, I want to store its key and value. The problem is, a value can be another hash array. Furthermore, that hash can contain key value pairs where the value is again another hash array, etc, etc. Also, I won't know how deeply nested each hash will be. To give an example:</p>
  
  <pre><code>{
    :key1 =&gt; 'value1',
    :key2 =&gt; 'value2',
    :key3 =&gt; {
       :key4 =&gt; 'value4',
       :key5 =&gt; 'value5'
     },
      :key6 =&gt; {
        :key7 =&gt; 'value7',
        :key8 =&gt; {
          :key9 =&gt; 'value9'
        }
      }
    }
  </code></pre>
  
  <p>..And so on. <i>What I want to do is save each key, value pair and the id of its parent.</i> I figure this will probably be done recursively, I'm just unsure how because I'm unfamiliar with recursive functions. I know how to iterate through the data normally:</p>
  
  <pre><code>  myHash.each {|key, value|
      ...Do something with the key and value ...
    }
  </code></pre>
  
  <p>And so I'm guessing the recursive call will be something like this:</p>
  
  <pre><code>def save_pair (myHash)
    myHash.each {|key, value|
      if(value.class != Hash) ? Pair.create(key, value) : save_pair(value)
    }
  end
  </code></pre>
  
  <p>This is untested, and I'm still unsure how to incorporate saving the parent ids regardless.</p>