Bird
Raised Fist0
Rubyprogramming~10 mins

Truthy and falsy values (only nil and false are falsy) in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Truthy and falsy values (only nil and false are falsy)
Start
↓
Evaluate value
↓
Is value nil or false?
Yes→Result: false (falsy)
No↓
Result: true (truthy)
↓
End
Ruby treats only nil and false as false in conditions; everything else is true.
Execution Sample
Ruby
values = [nil, false, 0, "", [], {}]
values.each do |v|
  puts v ? "truthy" : "falsy"
end
Checks each value and prints if it is truthy or falsy in Ruby.
Execution Table
IterationValueCondition (v ?)ResultOutput
1nilnil ? => falsefalsefalsy
2falsefalse ? => falsefalsefalsy
300 ? => truetruetruthy
4"""" ? => truetruetruthy
5[][] ? => truetruetruthy
6{}{} ? => truetruetruthy
💡 All values checked; only nil and false are falsy, others are truthy.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
vnilfalse0""[]{}{}{}
Key Moments - 3 Insights
Why is 0 considered truthy in Ruby when in some languages it is falsy?
Ruby treats only nil and false as falsy. Numbers like 0 are truthy, as shown in execution_table rows 3.
Is an empty string "" falsy in Ruby?
No, empty strings are truthy in Ruby, confirmed by execution_table row 4 where "" evaluates to true.
What about empty arrays or hashes, are they falsy?
Empty arrays [] and hashes {} are truthy in Ruby, as shown in execution_table rows 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when the value is false?
Atruthy
Bfalsy
Cerror
Dnil
💡 Hint
Check row 2 in execution_table where value is false and output is shown.
At which iteration does the condition become true for the first time?
AIteration 1
BIteration 2
CIteration 3
DIteration 4
💡 Hint
Look at execution_table rows 1-3 and see when condition changes from false to true.
If we add the value 'false' as a string ("false"), what would be its truthiness?
Atruthy
Bfalsy
Cnil
Derror
💡 Hint
Remember only nil and false (boolean) are falsy; strings are truthy as per concept.
Concept Snapshot
In Ruby, only nil and false are falsy.
Everything else (0, empty string, empty array, etc.) is truthy.
Use conditions like if value to check truthiness.
This differs from some other languages.
Remember: nil and false mean false; all else means true.
Full Transcript
This visual execution shows how Ruby treats values in conditions. We start by checking each value: nil, false, 0, empty string, empty array, and empty hash. Only nil and false evaluate as false, so the output is 'falsy' for them. All other values, including 0 and empty collections, are truthy and print 'truthy'. This is important because Ruby's rules for truthiness are simpler than some other languages. The variable 'v' changes each iteration to the next value in the list. The key moments clarify common confusions like why 0 is truthy and empty strings or arrays are not falsy. The quiz questions help reinforce these points by asking about specific steps in the execution table and what would happen with a string 'false'.

Practice

(1/5)
1. In Ruby, which of the following values is considered falsy?
easy
A. false
B. 0
C. "" (empty string)
D. [] (empty array)

Solution

  1. Step 1: Recall Ruby falsy values

    In Ruby, only false and nil are falsy.
  2. Step 2: Check each option

    0, empty string, and empty array are all truthy in Ruby.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    Falsy = false or nil [OK]
Hint: Only false and nil are falsy in Ruby [OK]
Common Mistakes:
  • Thinking 0 is falsy like in some other languages
  • Assuming empty string is falsy
  • Confusing empty array with falsy
2. Which of the following is the correct way to check if a variable x is truthy in Ruby?
easy
A. if x == true
B. if x
C. if x != nil
D. if x == nil

Solution

  1. Step 1: Understand Ruby truthy check

    In Ruby, if x automatically checks if x is truthy (not false or nil).
  2. Step 2: Analyze options

    if x == true only matches true, missing other truthy values; if x != nil treats false as truthy (false != nil); if x == nil checks the opposite of truthy.
  3. Final Answer:

    if x -> Option B
  4. Quick Check:

    Use if x for truthy check [OK]
Hint: Use 'if x' to check truthiness simply [OK]
Common Mistakes:
  • Using 'if x == true' which misses other truthy values
  • Using 'if x != nil' which treats false as truthy
  • Checking 'if x == nil' to test truthiness
3. What will be the output of this Ruby code?
value = 0
if value
  puts "Truthy"
else
  puts "Falsy"
end
medium
A. No output
B. Falsy
C. Error
D. Truthy

Solution

  1. Step 1: Identify the value and condition

    The variable value is assigned 0. The if value checks if value is truthy.
  2. Step 2: Recall Ruby truthy rules

    In Ruby, 0 is truthy, so the if branch runs.
  3. Final Answer:

    Truthy -> Option D
  4. Quick Check:

    0 is truthy in Ruby [OK]
Hint: Remember 0 is truthy in Ruby, unlike some languages [OK]
Common Mistakes:
  • Assuming 0 is falsy like in other languages
  • Expecting else branch to run
  • Confusing output with error
4. The following Ruby code is intended to print "Falsy" only when var is falsy. What is wrong?
var = nil
if var == false
  puts "Falsy"
else
  puts "Truthy"
end
medium
A. It should check 'if var == nil' only
B. Syntax error in the if statement
C. It misses the case when var is nil, which is also falsy
D. It should use 'var != false' instead

Solution

  1. Step 1: Understand falsy values in Ruby

    Both false and nil are falsy in Ruby.
  2. Step 2: Analyze the condition

    The code checks only var == false, so it misses nil which is also falsy.
  3. Final Answer:

    It misses the case when var is nil, which is also falsy -> Option C
  4. Quick Check:

    Falsy = false or nil [OK]
Hint: Check both false and nil for falsy, not just false [OK]
Common Mistakes:
  • Checking only for false and ignoring nil
  • Confusing syntax errors with logic errors
  • Using wrong comparison operators
5. You want to filter out falsy values from this array in Ruby:
arr = [false, nil, 0, "", [], true]
Which code correctly returns only truthy values?
hard
A. arr.select { |v| v }
B. arr.reject { |v| v }
C. arr.select { |v| v != false }
D. arr.select { |v| v == true }

Solution

  1. Step 1: Understand filtering truthy values

    Using select { |v| v } keeps only truthy values because v in condition is true for truthy values.
  2. Step 2: Analyze other options

    arr.select { |v| v != false } keeps nil (nil != false); arr.reject { |v| v } rejects truthy values; arr.select { |v| v == true } keeps only true, missing other truthy values.
  3. Final Answer:

    arr.select { |v| v } -> Option A
  4. Quick Check:

    Use select with block returning value for truthy filter [OK]
Hint: Use select { |v| v } to keep truthy values [OK]
Common Mistakes:
  • Using reject instead of select
  • Checking explicitly for false instead of truthiness
  • Selecting only true, missing other truthy values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard