Challenge - 5 Problems
Nil Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Understanding nil in conditional expressions
What is the output of this Ruby code snippet?
Ruby
value = nil if value puts "Value is true" else puts "Value is false or nil" 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
Nil and method calls
What happens when you call a method on nil in Ruby?
Ruby
result = nil.to_s puts result
Attempts:
2 left
💡 Hint
Check what methods nil supports in Ruby.
✗ Incorrect
Calling to_s on nil returns an empty string "".
❓ Predict Output
advanced2:00remaining
Nil in array operations
What is the output of this Ruby code?
Ruby
arr = [1, nil, 3] filtered = arr.compact puts filtered.inspect
Attempts:
2 left
💡 Hint
The
compact method removes nil elements from arrays.✗ Incorrect
The compact method returns a new array without any nil values.
❓ Predict Output
advanced2:00remaining
Nil and equality checks
What is the output of this Ruby code?
Ruby
a = nil b = false puts a == b puts a.nil? puts b.nil?
Attempts:
2 left
💡 Hint
Check how nil compares to false and how the nil? method works.
✗ Incorrect
nil == false is false because they are different objects.nil.nil? is true because it is nil.false.nil? is false because false is not nil.
🧠 Conceptual
expert2:00remaining
Nil and safe navigation operator
What is the output of this Ruby code using the safe navigation operator (&.)?
Ruby
person = nil name = person&.name puts name.nil?
Attempts:
2 left
💡 Hint
The safe navigation operator prevents errors when calling methods on nil.
✗ Incorrect
The safe navigation operator &. returns nil instead of raising an error if the object is nil.
So name is nil, and name.nil? returns true.