nil, true, false & Truthiness
Truthiness is the rule Ruby uses to decide whether a value counts as "yes" in a condition — and in Ruby the only falsy values are nil (nothing) and false ; literally everything else is truthy.
Learn nil, true, false & Truthiness in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
That means 0 and "" are truthy — a fact that trips up newcomers from other languages. You'll also learn nil? , safe navigation &. , and ||= .
What You'll Learn in This Lesson
1️⃣ The Only Two Falsy Values
Commit this to memory: in Ruby a condition is "false" only when the value is nil or false . Every other object — including 0 , "" , and [] — is truthy and will run an if block.
2️⃣ Checking for nil & Safe Navigation
To ask "is this nothing?", call .nil? — it's true only for nil , distinguishing it from false . When a value might be nil and you want to call a method anyway, the safe navigation operator &. returns nil instead of raising NoMethodError .
3️⃣ ||= Memoization & nil Conversions
x ||= y assigns only when x is currently nil or false — perfect for caching expensive results and for defaults. And nil converts politely: nil.to_s is "" , nil.to_a is [] , and nil.to_i is 0 .
Your turn. Fill in each ___ blank with the right operator, then run it.
Combine the falsy rules: treat an empty string as missing, then fall back to a default with || . Run with ruby greet.rb .
📋 Quick Reference — Truthiness
Practice quiz
Which values are the ONLY falsy values in Ruby?
- 0 and ""
- nil and false
- nil, false, and 0
- nil and ""
Answer: nil and false. In Ruby only nil and false are falsy; everything else, including 0 and "", is truthy.
What does the expression: if 0 then "yes" end evaluate to?
- nil
- "yes"
- false
- an error
Answer: "yes". 0 is truthy in Ruby, so the if block runs and returns "yes".
What does nil.to_s return?
- nil
- "nil"
- an empty string ""
- a NoMethodError
Answer: an empty string "". nil converts gracefully: nil.to_s is "", nil.to_a is [], and nil.to_i is 0.
What does nil&.upcase return?
- nil
- ""
- raises NoMethodError
- "NIL"
Answer: nil. Safe navigation &. returns nil instead of raising when the receiver is nil.
Which method tells nil apart from false?
- empty?
- nil?
- false?
- blank?
Answer: nil?. value.nil? is true only for nil, distinguishing it from false.
After @count = nil; @count ||= 0; @count ||= 99, what is @count?
- nil
- 0
- 99
- false
Answer: 0. ||= assigns only when nil/false. @count becomes 0, then since 0 is truthy the second ||= is ignored.
What does "ada"&.upcase return?
- nil
- "ada"
- "ADA"
- an error
Answer: "ADA". The receiver isn't nil, so &. simply calls upcase normally, giving "ADA".
What does x ||= y expand to?
- x = x && y
- x || (x = y)
- x = y
- x && (x = y)
Answer: x || (x = y). x ||= y means x || (x = y): assign y only if x is currently nil or false.
Is an empty array [] truthy or falsy in Ruby?
- falsy
- truthy
- nil
- it raises an error
Answer: truthy. Empty collections are still objects, so [] is truthy. Use .empty? to test emptiness.
Calling a method on nil without a guard typically raises which error?
- ArgumentError
- NameError
- NoMethodError
- TypeError
Answer: NoMethodError. nil.upcase raises NoMethodError; use &. or a .nil? check to guard against it.