title: Ruby Style: How to check whether a nested hash element exists
body: <p>Consider a "person" stored in a hash. Two examples are:</p>
  
  <pre><code>fred = {:person =&gt; {:name =&gt; "Fred", :spouse =&gt; "Wilma", :children =&gt; {:child =&gt; {:name =&gt; "Pebbles"}}}}
  slate = {:person =&gt; {:name =&gt; "Mr. Slate", :spouse =&gt; "Mrs. Slate"}} 
  </code></pre>
  
  <p>If the "person" doesn't have any children, the "children" element is not present. So, for Mr. Slate, we can check whether he has parents:</p>
  
  <pre><code>slate_has_children = !slate[:person][:children].nil?
  </code></pre>
  
  <p>So, what if we don't know that "slate" is a "person" hash? Consider:</p>
  
  <pre><code>dino = {:pet =&gt; {:name =&gt; "Dino"}}
  </code></pre>
  
  <p>We can't easily check for children any longer:</p>
  
  <pre><code>dino_has_children = !dino[:person][:children].nil?
  NoMethodError: undefined method `[]' for nil:NilClass
  </code></pre>
  
  <p>So, how would you check the structure of a hash, especially if it is nested deeply (even deeper than the examples provided here)? Maybe a better question is: What's the "Ruby way" to do this?</p>