Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

Booleans and nil

Booleans 12 minIn Ruby only false and nil are falsy, so zero, empty strings and empty arrays all count as true in a condition.

Booleans and nil

Bean Cart's script checks if cups_left to decide whether to take another order. With 0 cups left, it still says yes. A programmer from Python or JavaScript expects 0 to count as "false", but Ruby has a much shorter list of false things, and that list is worth memorising.

The rule

Only false and nil are falsy; everything else is truthy.

Ruby's booleans are the two objects true and false. They belong to the classes TrueClass and FalseClass; there is no single Boolean class.

nil means "no value here". It is the only object of class NilClass. You get it when something is missing:

MENU = { latte: 4.50 }
MENU[:chai]      # => nil  (no such key)
[1, 2][5]        # => nil  (no such index)
puts "hi"        # returns nil

In a condition, Ruby does not need a real boolean. It only asks: "is this false or nil?" If not, the condition passes. So all of these are truthy: 0, "", [], {}, "false", 0.0.

cups_left = 0
if cups_left
  puts "Take order"   # this runs: 0 is truthy
end

if cups_left > 0      # the real question
  puts "Take order"
end

To test for nil specifically, ask the object: price.nil?. To test for "empty", ask that instead: name.empty?, orders.empty?, cups_left.zero?.

nil has safe conversions that come up often: nil.to_s is "", nil.to_a is [], and nil.to_i is 0. And the safe navigation operator &. calls a method only if the object is not nil:

customer = nil
customer&.upcase   # => nil, instead of a NoMethodError

The shortcut

To predict any condition, run a two-item checklist: is the value false? Is it nil? If neither, the condition is true, however "empty" the value looks.

Which Bean Cart checks pass?Step 1 of 4
  1. if cups_left with cups_left = 0

    Not false, not nil, so it passes. Probably a bug: the question is cups_left > 0.

Drills

Answer fast — trust the rule.

quick drill

Which of these values is falsy in Ruby?

quick drill

What does MENU[:chai] return when the hash has no :chai key?

quick drill

What is true.class?

quick drill

With name = nil, what does name&.upcase return?

quick drill

What does nil.to_a return?

Remember

  • Only false and nil are falsy; everything else, including 0 and "", is truthy.
  • Two-item checklist: false? nil? If neither, the condition passes.
  • Trap: if count when you meant if count > 0; test with nil?, empty? or zero? instead.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainZero cups, still open

Bean Cart uses if cups_left to decide whether to take an order, and it still takes orders when cups_left is 0. Explain why, fix the condition, and name the only two falsy values in Ruby.