Challenge - 5 Problems
Ruby Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of chained comparison with integers
What is the output of this Ruby code?
Ruby
result = 3 < 5 && 5 < 7 puts result
Attempts:
2 left
💡 Hint
Remember that && checks if both sides are true.
✗ Incorrect
The expression checks if 3 is less than 5 AND 5 is less than 7. Both are true, so the result is true.
❓ Predict Output
intermediate2:00remaining
Output of spaceship operator <=> with strings
What will this Ruby code output?
Ruby
puts 'apple' <=> 'banana'
Attempts:
2 left
💡 Hint
The spaceship operator returns -1 if left is less than right.
✗ Incorrect
Since 'apple' comes before 'banana' alphabetically, the operator returns -1.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
== compares values, eql? compares both value and type.
✗ Incorrect
5 == 5.0 is true because values are equal, but 5.eql?(5.0) is false because types differ (Integer vs Float).
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Check which range includes 85.
✗ Incorrect
85 is in the range 80...90 (80 to 89), so it prints 'B'.
🧠 Conceptual
expert2:00remaining
Behavior of <=> operator with nil values
What error or output results from this Ruby code?
Ruby
puts nil <=> 5
Attempts:
2 left
💡 Hint
The spaceship operator returns nil if values are not comparable.
✗ Incorrect
Comparing nil with a number using <=> returns nil because they are not comparable.