title: Nil and boolean in Ruby
body: <p>Can someone explain the reasoning being this? Just spent 30 mins trying to figure out why my boolean method returned <code>nil</code> and found out that in Ruby:</p>
  
  <pre><code>2.2.1 :001 &gt; nil &amp;&amp; true
   =&gt; nil
  2.2.1 :002 &gt; nil &amp;&amp; false
   =&gt; nil
  </code></pre>
  
  <p>Since <code>nil</code> is a falsey value, I would have expected the output of <code>nil &amp;&amp; true</code> to be false. Also it seems to go against the idea that conditional operators should return a boolean value.</p>
  
  <p>What is the rationale behind this? </p>
  
  <p>It makes sense that the boolean operator is not commutative:</p>
  
  <pre><code>nil &amp;&amp; false != false &amp;&amp; nil
  </code></pre>
  
  <p>For others seeing this, my issue was that in rails I had a statement like:</p>
  
  <pre><code>def some_method?
    object.attr &amp;&amp; object.attr &gt; something
  end
  </code></pre>
  
  <p>But when <code>object.attr</code> is nil, the function will be nil. Which is fine in most cases but when chaining boolean methods together, not so much. I just changed it to this instead:</p>
  
  <pre><code>def some_method?
    object.attr.present? &amp;&amp; object.attr &gt; something
  end
  </code></pre>
  
  <p>I could do the same thing in vanilla Ruby with:</p>
  
  <pre><code>def some_method?
    !!object.attr &amp;&amp; object.attr &gt; something
  end
  </code></pre>