Bird
Raised Fist0
Rubyprogramming~10 mins

Boolean values (true, false, nil) in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Boolean values (true, false, nil)
Start
↓
Evaluate expression
↓
Is value true?
Yes No↓
Use true
↓
Use false
↓
Use nil
↓
End
This flow shows how Ruby evaluates values as true, false, or nil and how each is treated in conditions.
Execution Sample
Ruby
a = true
b = false
c = nil
puts a
puts b
puts c
Assigns true, false, and nil to variables and prints them.
Execution Table
StepActionVariableValueOutput
1Assign true to aatrue
2Assign false to bbfalse
3Assign nil to ccnil
4Print aatruetrue
5Print bbfalsefalse
6Print ccnil
💡 All variables assigned and printed correctly; nil prints as an empty line.
Variable Tracker
VariableStartAfter 1After 2After 3Final
aundefinedtruetruetruetrue
bundefinedundefinedfalsefalsefalse
cundefinedundefinedundefinednilnil
Key Moments - 3 Insights
What does printing nil output in Ruby?
In Ruby, puts nil prints an empty line, as seen in step 6 of the execution_table.
Are true and false the only Boolean values in Ruby?
Ruby has true and false as Boolean values, but nil is also treated as false in conditions, as shown in the concept_flow where nil leads to a false branch.
What happens if a variable is not assigned before use?
Using an unassigned variable causes an error, but here all variables are assigned before printing, so no error occurs (see variable_tracker start values).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when printing variable c?
Anil
BNo visible output
Cfalse
Dtrue
💡 Hint
Check step 6 in execution_table where c is printed.
According to variable_tracker, what is the value of b after step 2?
Afalse
Bundefined
Cnil
Dtrue
💡 Hint
Look at the 'After 2' column for variable b in variable_tracker.
If we change a = true to a = nil, how would the output at step 4 change?
AIt would print 'true'
BIt would print 'false'
CIt would print 'nil'
DIt would cause an error
💡 Hint
Refer to how printing nil behaves in execution_table step 6.
Concept Snapshot
Boolean values in Ruby:
- true and false are Boolean literals.
- nil means 'no value' and acts like false in conditions.
- Printing true or false shows 'true' or 'false'.
- Printing nil shows an empty line.
- Use these values in conditions to control flow.
Full Transcript
This lesson shows how Ruby uses true, false, and nil as Boolean values. Variables a, b, and c are assigned true, false, and nil respectively. Printing a shows 'true', printing b shows 'false', and printing c shows an empty line because that's Ruby's representation of nil when printed with puts. The flow diagram explains how Ruby checks if a value is true, false, or nil to decide what to do. The variable tracker shows how variables change step by step. Key moments clarify printing nil and how nil acts like false in conditions. The quiz tests understanding of outputs and variable states.

Practice

(1/5)
1. In Ruby, which of the following represents a value that means "nothing" or "no value"?
easy
A. nil
B. true
C. false
D. 0

Solution

  1. Step 1: Understand the meaning of nil

    In Ruby, nil is used to represent the absence of any value, like an empty box.
  2. Step 2: Compare with other options

    true and false are Boolean values meaning yes or no, but not "no value". 0 is a number, not a special value for nothing.
  3. Final Answer:

    nil -> Option A
  4. Quick Check:

    nil means no value [OK]
Hint: Remember: nil means no value or empty box [OK]
Common Mistakes:
  • Confusing false with nil
  • Thinking 0 means no value
  • Mixing true and nil meanings
2. Which of the following is the correct way to write a Boolean false value in Ruby?
easy
A. False
B. FalseValue
C. FALSE
D. false

Solution

  1. Step 1: Recall Ruby syntax for Boolean values

    Ruby uses lowercase true and false for Boolean values.
  2. Step 2: Check each option

    False and FALSE are not valid Ruby keywords. FalseValue is not a keyword but a name.
  3. Final Answer:

    false -> Option D
  4. Quick Check:

    Boolean false is lowercase false [OK]
Hint: Boolean keywords are lowercase in Ruby [OK]
Common Mistakes:
  • Using capitalized False or TRUE
  • Adding extra words to Boolean values
  • Confusing symbols with keywords
3. What will the following Ruby code print?
value = nil
if value
  puts "Yes"
else
  puts "No"
end
medium
A. nil
B. Yes
C. No
D. Error

Solution

  1. Step 1: Understand how Ruby treats nil in conditions

    In Ruby, nil is treated as false in conditions.
  2. Step 2: Follow the if-else logic

    Since value is nil, the if value condition is false, so the else branch runs, printing "No".
  3. Final Answer:

    No -> Option C
  4. Quick Check:

    nil acts as false in if [OK]
Hint: nil and false both act as false in conditions [OK]
Common Mistakes:
  • Thinking nil is true
  • Expecting nil to print itself
  • Confusing nil with empty string
4. Find the error in this Ruby code snippet:
if false
  puts "No"
elsif nil
  puts "Maybe"
else
  puts "Yes"
end
medium
A. The condition elsif nil never runs
B. No error, prints "Yes"
C. The code prints "Maybe" incorrectly
D. Syntax error: missing end

Solution

  1. Step 1: Check syntax correctness

    The code has matching if, elsif, and end, so no syntax error.
  2. Step 2: Evaluate conditions in order

    false is false, nil is also false, so both conditions fail and else runs, printing "Yes".
  3. Final Answer:

    No error, prints "Yes" -> Option B
  4. Quick Check:

    False and nil skip to else [OK]
Hint: False and nil conditions skip to else branch [OK]
Common Mistakes:
  • Thinking elsif nil runs
  • Expecting syntax error
  • Confusing elsif with else
5. You want to check if a variable status is either true or nil in Ruby. Which condition correctly does this?
hard
A. if status == true || status == nil
B. if status == true && status == nil
C. if status || nil
D. if status != false

Solution

  1. Step 1: Understand the condition requirements

    You want to check if status is either true OR nil, so use logical OR (||).
  2. Step 2: Analyze each option

    if status == true || status == nil uses status == true || status == nil, which matches the requirement. if status == true && status == nil uses AND, which can never be true for both at once. if status || nil is incorrect syntax and logic. if status != false checks if status is not false, which includes other values too.
  3. Final Answer:

    if status == true || status == nil -> Option A
  4. Quick Check:

    Use OR (||) to check either condition [OK]
Hint: Use || to check if either condition is true [OK]
Common Mistakes:
  • Using && instead of ||
  • Confusing != false with true or nil
  • Writing invalid condition syntax
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