title: Ruby: Safe-navigation operator, undefined method `call`
body: <p>I am trying to compare a number literal with the return value of a function that could return <code>nil</code> or a numeric. Consider this:</p>
  
  <pre><code>def unreliable
    [nil, 42].sample  
  end
  
  unreliable &gt; 10
  </code></pre>
  
  <p>This will blow up 50% of the time with <code>NoMethodError: undefined method '&gt;' for nil:NilClass</code>. So I tried this:</p>
  
  <pre><code>unreliable&amp;.(:&gt;, 10)
  </code></pre>
  
  <p>That approach does the <code>nil</code> guarding I expect, but I get this when <code>unreliable</code> returns <code>42</code>:</p>
  
  <pre><code>NoMethodError: undefined method `call' for 42:Fixnum
  </code></pre>
  
  <p>I suspect this has to do with the quirks of allowing only one instance to exist for each <code>Numeric</code>, see <a href="https://ruby-doc.org/core-2.4.1/Numeric.html" rel="nofollow noreferrer">here</a>. And I know I can do this:</p>
  
  <pre><code>foo = unreliable
  foo &amp;&amp; foo &gt; 10
  </code></pre>
  
  <p>But is there a way to use the safe navigation operator with a numeric and <code>:&gt;</code>, <code>:&lt;</code>, <code>:==</code>, <code>:+</code>, <code>:-</code>, <code>:/</code>, <code>:*</code>, etc?</p>
  
  <hr>
  
  <p><strong>Edit</strong>: The focus on <code>Numeric</code> in my question is a red herring. See @Jörg's answer. I was confusing Rails's <code>try</code> syntax with the safe-navigation operator's syntax.</p>