Challenge - 5 Problems
Boolean Mastery in Ruby
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code?
Consider the following Ruby code snippet. What will it print when run?
Ruby
value = nil if value puts "true branch" else puts "false branch" end
Attempts:
2 left
💡 Hint
Remember that in Ruby, only false and nil are treated as false in conditionals.
✗ Incorrect
In Ruby, nil is treated as false in conditionals, so the else branch runs.
❓ Predict Output
intermediate2:00remaining
What does this expression return?
What is the value of the variable
result after running this Ruby code?Ruby
result = (false || nil) && true
Attempts:
2 left
💡 Hint
Recall how Ruby evaluates && and || with false and nil.
✗ Incorrect
Ruby evaluates false || nil as nil because false is falsey, so it returns the second operand. Then nil && true returns nil because nil is falsey.
🔧 Debug
advanced2:00remaining
Why does this code raise an error?
This Ruby code raises an error. What is the cause?
Ruby
def check_value(val) if val == true || val == false || val == nil puts "Valid boolean or nil" else puts "Invalid value" end end check_value(nil)
Attempts:
2 left
💡 Hint
Check how Ruby compares values with == and how nil behaves.
✗ Incorrect
The code runs without error because == works with nil and booleans. The condition is true for nil, so it prints "Valid boolean or nil".
❓ Predict Output
advanced2:00remaining
What is the output of this Ruby code involving nil and boolean?
What does this Ruby code print?
Ruby
value = false puts value || "default"
Attempts:
2 left
💡 Hint
Remember how || returns the first truthy value.
✗ Incorrect
Since false is falsey, value || "default" returns "default".
🧠 Conceptual
expert2:00remaining
Which Ruby value is treated as false in conditionals?
In Ruby, which of the following values are treated as false in conditional expressions?
Attempts:
2 left
💡 Hint
Think about what Ruby considers falsey.
✗ Incorrect
In Ruby, only false and nil are falsey. Everything else, including 0, is truthy.