title: How can I return nil instead of an error from a missing hash key?
body: <p>I have a hash, and I'm looking for a certain key inside it. But sometimes <code>hash</code> itself can be nil. In this event, I don't want <code>hash["key"]</code> to return an <code>undefined method '[]' for nil:NilClass</code> error. I want it to return <code>nil</code> instead.</p>
  
  <p>With other similar situations, I can use <code>try</code>, like this: <code>nil.try(:key) =&gt; nil</code> but I can't get the same method to work with my hash.</p>
  
  <p>In other words, I'm looking for a method that results in something like this:</p>
  
  <pre><code>&gt; @hash = nil
  &gt; @hash.try(:["key"])
  =&gt; nil
  </code></pre>