title: Ruby Oneline Rescue
body: <p>I recently learned that you can use <code>rescue</code> on a line of code in case something goes wrong on that line (see <a href="http://www.rubyinside.com/21-ruby-tricks-902.html">http://www.rubyinside.com/21-ruby-tricks-902.html</a> Tip #21). I have some code that used to look like this:</p>
  
  <pre><code>if obj['key'] &amp;&amp; obj['key']['key2'] &amp;&amp; obj['key']['key2']['name']
    name = obj['key']['key2']['name']
  else
    name = ''
  end
  </code></pre>
  
  <p>With the <code>rescue</code> method, I believe I can change that code into something like this:</p>
  
  <pre><code>name = obj['key']['key2']['name'] rescue ''
  </code></pre>
  
  <p>If a nil exception is thrown at any level of accessing the hash, it should get caught by the rescue and give me '', which is what I want. I could also choose to set name to <code>nil</code> if that were the desired behavior.</p>
  
  <p>Is there any known danger in doing this? I ask because this seems too good to be true. I have so much ugly code that I'd love to get rid of that looks like the first code example.</p>