Bird
0
0

What will be the output of this Ruby code snippet?

medium📝 Predict Output Q4 of 15
Ruby - Operators and Expressions

What will be the output of this Ruby code snippet?

puts 3 <=> 7
puts 7 <=> 7
puts 10 <=> 2
A1 0 -1
B-1 0 1
Cnil 0 1
D-1 1 0
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate 3 <=> 7

    3 is less than 7, so returns -1.
  2. Step 2: Evaluate 7 <=> 7

    Both are equal, so returns 0.
  3. Step 3: Evaluate 10 <=> 2

    10 is greater than 2, so returns 1.
  4. Final Answer:

    -1 0 1 -> Option B
  5. Quick Check:

    Less than: -1, Equal: 0, Greater than: 1 [OK]
Quick Trick: Less: -1, Equal: 0, Greater: 1 [OK]
Common Mistakes:
MISTAKES
  • Mixing up the order of outputs
  • Assuming nil is returned for numeric comparisons
  • Confusing the meaning of return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes