&&, ||, and, or and safe navigation

Two sets of operators

Ruby has && and || with the precedence programmers expect, and and and or with a much lower precedence, lower than assignment. The second set exists for control flow, not for values, and mixing the two is a classic bug:

ruby -e 'x = true and false; puts x'

true

The line reads as (x = true) and false: the assignment happens first, so x is true and the false is thrown away. With && the same line is false:

ruby -e 'puts (true && false).inspect; puts (true and false).inspect'

false
false

Parentheses make both expressions evaluate to false; without them, only the && form assigns what the reader expects. Use && and || in expressions and keep and/or for a chain of statements, or drop them entirely.

The default with ||=

||= assigns only when the variable is nil or false. It is the short way to fill a default once, and it reads left to right:

ruby -e 'x = nil; x ||= 5; puts x'

5

A second x ||= 7 leaves the five alone. The pattern is common in a method that takes an optional argument, and it is also a trap with booleans: enabled ||= true turns a deliberate false into true, because false is falsy. For a boolean default, test nil? instead.

Safe navigation

&. calls a method only when the receiver is not nil; otherwise the whole expression is nil. It removes one level of if from a chain that may start with a missing value:

ruby -e 'x = nil; puts x&.length.inspect; puts "abc"&.length'

nil
3

The number of safe-navigation calls in a method is a measure of how uncertain the data is. A long a&.b&.c&.d chain usually means the first value should have been validated once, where it entered the program.

What to watch for

An &. chain returns nil for a missing middle value, and a later method without the ampersand raises. Keep the ampersands consistent for the whole chain or the error appears at the step after the check.

&.length on a hash is not hash[:length]. The ampersand goes before the dot, so value&.[](:key) is the form for a subscript, and it is rarely worth writing.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? x = true and false puts x

    Answer

    true

    The assignment binds tighter than and, so x is true and the false is thrown away.

  2. What does this program print? x = true && false puts x

    Answer

    false

    && binds tighter than the assignment, so the expression is false and x holds false.

  3. Which operator has the lowest precedence?

    Answer

    and

    and and or sit below the assignment, which is why x = true and false surprises a reader.

  4. What does x ||= 5 do?

    Answer

    Assigns 5 only when x is nil or false

    The || form assigns only when the current value is falsy.

  5. What does this program print? x = nil x ||= 5 x ||= 7 puts x

    Answer

    5

    The first line fills the nil, and the second leaves the five alone.

  6. What does this program print? enabled = false enabled ||= true puts enabled

    Answer

    true

    false is falsy, so ||= overwrites the deliberate false, which is the trap the lesson warns about.

  7. Which test gives a boolean a default without overwriting a deliberate false?

    Answer

    enabled = true if enabled.nil?

    nil? is false for a deliberate false, so the value is kept.

  8. What does this program print? x = nil puts x&.length.inspect

    Answer

    nil

    The safe navigation stops at nil and the whole expression is nil; inspect writes the word nil.

  9. What does "abc"&.length return?

    Answer

    3

    The receiver is not nil, so the method runs as usual.

  10. How do you call [] through safe navigation?

    Answer

    value&.[](:key)

    The ampersand goes before the dot, and [] is written as a method name.

Article author: Arthur Isaev