0
0
Rubyprogramming~20 mins

Nil as the absence of value in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nil Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AValue is true
Bnil
CValue is false or nil
DError: undefined variable
Attempts:
2 left
💡 Hint
Remember that in Ruby, only false and nil are treated as false in conditionals.
Predict Output
intermediate
2:00remaining
Nil and method calls
What happens when you call a method on nil in Ruby?
Ruby
result = nil.to_s
puts result
Anil
B"" (empty string)
CNoMethodError
Dfalse
Attempts:
2 left
💡 Hint
Check what methods nil supports in Ruby.
Predict Output
advanced
2:00remaining
Nil in array operations
What is the output of this Ruby code?
Ruby
arr = [1, nil, 3]
filtered = arr.compact
puts filtered.inspect
A[1, 3]
BError: undefined method 'compact'
C[nil]
D[1, nil, 3]
Attempts:
2 left
💡 Hint
The compact method removes nil elements from arrays.
Predict Output
advanced
2: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?
A
false
true
false
B
true
true
true
C
false
false
false
D
true
false
true
Attempts:
2 left
💡 Hint
Check how nil compares to false and how the nil? method works.
🧠 Conceptual
expert
2: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?
ANoMethodError
Bfalse
Cnil
Dtrue
Attempts:
2 left
💡 Hint
The safe navigation operator prevents errors when calling methods on nil.