0
0
Rubyprogramming~20 mins

Comparison operators in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of chained comparison with integers
What is the output of this Ruby code?
Ruby
result = 3 < 5 && 5 < 7
puts result
ASyntaxError
Btrue
Cfalse
Dnil
Attempts:
2 left
💡 Hint
Remember that && checks if both sides are true.
Predict Output
intermediate
2:00remaining
Output of spaceship operator <=> with strings
What will this Ruby code output?
Ruby
puts 'apple' <=> 'banana'
A-1
B0
C1
Dnil
Attempts:
2 left
💡 Hint
The spaceship operator returns -1 if left is less than right.
Predict Output
advanced
2:00remaining
Result of comparing different types with == and eql?
What is the output of this Ruby code?
Ruby
a = 5
b = 5.0
puts a == b
puts a.eql?(b)
A
true
false
B
false
false
C
true
true
D
false
true
Attempts:
2 left
💡 Hint
== compares values, eql? compares both value and type.
Predict Output
advanced
2:00remaining
Output of combined comparison with case statement
What will this Ruby code output?
Ruby
score = 85
case score
when 90..100 then puts 'A'
when 80...90 then puts 'B'
else puts 'F'
end
ASyntaxError
BA
CF
DB
Attempts:
2 left
💡 Hint
Check which range includes 85.
🧠 Conceptual
expert
2:00remaining
Behavior of <=> operator with nil values
What error or output results from this Ruby code?
Ruby
puts nil <=> 5
ATypeError
BArgumentError
Cnil
Dfalse
Attempts:
2 left
💡 Hint
The spaceship operator returns nil if values are not comparable.