Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Side Note</strong></p> <p>It is worth noting that <code>and</code> and <code>&amp;&amp;</code> are not equivalent.</p> <p><code>and</code> is a flow control operator while <code>&amp;&amp;</code> is a Boolean operator. What is the difference?</p> <p>One example of the differences, using <code>and</code>, you can assign values.</p> <pre><code>value = nil and value / 42 </code></pre> <p>This would fail if you tried it as below</p> <pre><code>value = nil &amp;&amp; value / 42 </code></pre> <p><strong>Original Question 1</strong></p> <blockquote> <p>Assuming I want to evaluate one-liners with a return statement (similar to a certain > commonly used shell/bash expression), would this be a recommended way to do it, or is there > a more recommended approach?</p> </blockquote> <p>The way I have always seen this done in Ruby is this:</p> <pre><code>value if conditional </code></pre> <p>This will return the value if the conditional is met and <code>nil</code> otherwise. No need for the <code>return</code> statement if this is the last command in the function!</p> <p>If you are using this for an early exit to a function, I prefer using an <code>unless</code>. For instance...</p> <pre><code>return unless arguments_are_valid </code></pre> <p><strong>Original Question 2</strong></p> <blockquote> <p>foo method causes a syntax error. bar doesn't. Why?</p> </blockquote> <p>It's a matter of operator precedence. See the example below showing how they are evaluated.</p> <pre><code>(true &amp;&amp; return) false (true) and (return false) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload