Truthiness: only false and nil

Only false and nil are falsy

Every Ruby value counts as true in a condition except two: false and nil. nil means no value, the answer you get when something is missing.

Both lines below stay silent because their conditions are falsy, so only the last line prints:

puts "hi" if false puts "hi" if nil puts "done"

ruby truthy.rb

done

Zero is truthy

In some languages zero behaves like false, but not in Ruby. The number zero is an ordinary truthy value, so a counter reaching zero still passes the check.

if 0 puts "zero is truthy" end

ruby zero.rb

zero is truthy

Empty is truthy

Empty values are truthy as well. An empty string and an empty array both pass the check: only false and nil can stop a condition.

if "" puts "empty string is truthy" end if [] puts "empty array is truthy" end

ruby empty.rb

empty string is truthy
empty array is truthy

Use it for guards

Truthiness shines in small guard lines that handle the missing case first. Ask whether a value is nil, print a fallback, and let the rest of the code assume a real value.

name = nil puts "guest" if name.nil?

ruby guard.rb

guest

Flip the variable to a real string and the guard stays silent. Lesson 3.1 builds full if and else branches on top of this idea.

Questions

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

  1. Which values are falsy in Ruby?

    Answer

    false and nil

    Only false and nil are falsy; zero, an empty string and an empty array are truthy.

  2. What does this program print? puts "hi" if 0

    Answer

    hi

    Zero is truthy in Ruby, so the condition passes and puts runs.

  3. What does this program print? puts "hi" if nil puts "done"

    Answer

    done

    nil is falsy, so the first line stays silent and only the second prints.

  4. What does this program print? puts "empty" if ""

    Answer

    empty

    An empty string is a value, and only false and nil are falsy.

  5. What does this program print? values = [] puts "empty" if values.empty?

    Answer

    empty

    empty? returns true for an empty array, and true passes the condition.

  6. What does nil mean?

    Answer

    The absence of a value

    nil is the answer when something is missing; it is not zero and not an empty string.

  7. What does this program print? name = nil puts name.nil? puts name.to_s.empty?

    Answer

    true / true

    nil answers true to nil?, and to_s turns it into an empty string, which is empty.

  8. Which of these conditions is false?

    Answer

    if nil

    Every value except false and nil passes, so only if nil is false.

  9. In which other language does zero behave like false, as this lesson says?

    Answer

    Python and C treat 0 as false, unlike Ruby

    Python, C and JavaScript all treat 0 as false; Ruby does not.

  10. What is a guard line such as puts "guest" if name.nil? for?

    Answer

    Handling the missing case before the rest of the code assumes a value

    The guard deals with the missing value first, so the code below can use a real one.

Article author: Arthur Isaev