Bird
Raised Fist0
Rubyprogramming~10 mins

Spaceship operator (<=>) in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Spaceship operator (<=>)
Compare a and b
↓
Is a < b?
Yes→Return -1
No↓
Is a == b?
Yes→Return 0
No↓
Return 1 (a > b)
The spaceship operator compares two values and returns -1, 0, or 1 depending on whether the first is less than, equal to, or greater than the second.
Execution Sample
Ruby
result = 5 <=> 10
puts result
Compares 5 and 10, then prints the comparison result.
Execution Table
StepExpressionComparisonResult
15 <=> 105 < 10-1
2puts resultPrints result-1
💡 Comparison done, result printed, program ends.
Variable Tracker
VariableStartAfter ComparisonFinal
resultnil-1-1
Key Moments - 3 Insights
Why does 5 <=> 10 return -1 instead of true or false?
The spaceship operator returns -1, 0, or 1 to show order, not a boolean. See execution_table step 1 where 5 < 10 returns -1.
What happens if both values are equal?
If values are equal, the operator returns 0. This is shown in the flow where equality leads to 0.
Why is the result stored in a variable before printing?
Storing allows reuse or further checks. The execution_table shows result assigned before printing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of 5 <=> 10 at step 1?
A0
B-1
C1
Dtrue
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step is the comparison result printed?
AStep 1
BAfter program ends
CStep 2
DBefore comparison
💡 Hint
Look at the 'Expression' column for 'puts result' in execution_table.
If we compare 10 <=> 5, what would the result be?
A1
B0
C-1
Dnil
💡 Hint
Recall the spaceship operator returns 1 if the left is greater than the right.
Concept Snapshot
Spaceship operator (<=>) compares two values.
Returns -1 if left < right.
Returns 0 if left == right.
Returns 1 if left > right.
Useful for sorting and comparisons.
Syntax: a <=> b
Full Transcript
The spaceship operator in Ruby compares two values and returns -1, 0, or 1. It returns -1 if the first value is less than the second, 0 if they are equal, and 1 if the first is greater. For example, 5 <=> 10 returns -1 because 5 is less than 10. This result can be stored in a variable and printed. This operator is helpful when you want to know the order between two values, not just if one is true or false compared to the other.

Practice

(1/5)
1.

What does the Ruby spaceship operator <=> return when comparing two values?

easy
A. true if left is smaller, false if equal, nil if left is bigger
B. -1 if left is smaller, 0 if equal, 1 if left is bigger
C. The sum of both values
D. A boolean indicating if values are equal

Solution

  1. Step 1: Understand the spaceship operator purpose

    The spaceship operator compares two values and returns an integer indicating their order.
  2. Step 2: Recall the return values

    It returns -1 if the left value is smaller, 0 if both are equal, and 1 if the left is bigger.
  3. Final Answer:

    -1 if left is smaller, 0 if equal, 1 if left is bigger -> Option B
  4. Quick Check:

    Spaceship operator returns -1, 0, or 1 [OK]
Hint: Remember: -1 smaller, 0 equal, 1 bigger [OK]
Common Mistakes:
  • Confusing return values with booleans
  • Thinking it returns a sum or boolean
  • Mixing up which side is compared
2.

Which of the following is the correct syntax to compare a and b using the spaceship operator in Ruby?

?
easy
A. a => b
B. a =< b
C. a <= b
D. a <=> b

Solution

  1. Step 1: Identify the spaceship operator syntax

    The spaceship operator is written as <=> between two values.
  2. Step 2: Match the correct syntax

    Only a <=> b uses the correct operator symbol and order.
  3. Final Answer:

    a <=> b -> Option D
  4. Quick Check:

    Correct operator is <=> [OK]
Hint: Look for <=> exactly between variables [OK]
Common Mistakes:
  • Using => which is a hash rocket
  • Using <= or =< which are comparison operators
  • Swapping operator symbols incorrectly
3.

What is the output of the following Ruby code?

puts 5 <=> 10
puts 10 <=> 10
puts 15 <=> 10
medium
A. false true false
B. 1 0 -1
C. -1 0 1
D. 10 10 10

Solution

  1. Step 1: Evaluate each comparison

    5 <=> 10 returns -1 because 5 is smaller than 10.
    10 <=> 10 returns 0 because both are equal.
    15 <=> 10 returns 1 because 15 is bigger than 10.
  2. Step 2: Confirm output lines

    Each puts prints the result on a new line, so output is three lines: -1, 0, 1.
  3. Final Answer:

    -1 0 1 -> Option C
  4. Quick Check:

    5 < 10 = -1, equal = 0, 15 > 10 = 1 [OK]
Hint: Remember: smaller = -1, equal = 0, bigger = 1 [OK]
Common Mistakes:
  • Mixing up the order of output
  • Expecting boolean true/false instead of -1,0,1
  • Confusing <=> with < or > operators
4.

Identify the error in this Ruby code snippet using the spaceship operator:

result = 7 <=>
3
puts result
medium
A. Syntax error due to line break in operator
B. Returns nil because 7 and 3 are not comparable
C. Returns 1 as expected
D. Runtime error due to undefined variable

Solution

  1. Step 1: Check operator usage across lines

    The spaceship operator <=> must be on the same line between operands; splitting it causes syntax error.
  2. Step 2: Understand Ruby syntax rules

    Ruby does not allow breaking operators across lines without proper continuation, so this code raises a syntax error.
  3. Final Answer:

    Syntax error due to line break in operator -> Option A
  4. Quick Check:

    Operators must not be split across lines [OK]
Hint: Keep <=> on one line between values [OK]
Common Mistakes:
  • Splitting operator across lines
  • Assuming nil return for invalid syntax
  • Confusing runtime errors with syntax errors
5.

Given the array arr = [3, 1, 4, 1, 5], which code snippet correctly sorts the array in descending order using the spaceship operator?

hard
A. arr.sort { |a, b| b <=> a }
B. arr.sort { |a, b| a == b }
C. arr.sort_by { |x| x <=> 0 }
D. arr.sort { |a, b| a <=> b }

Solution

  1. Step 1: Understand sorting with spaceship operator

    The block compares two elements a and b. To sort descending, compare b to a.
  2. Step 2: Analyze each option

    arr.sort { |a, b| a <=> b } sorts ascending (a <=> b).
    arr.sort { |a, b| b <=> a } sorts descending (b <=> a).
    arr.sort_by { |x| x <=> 0 } misuses sort_by with spaceship operator.
    arr.sort { |a, b| a == b } returns boolean, invalid for sorting.
  3. Final Answer:

    arr.sort { |a, b| b <=> a } -> Option A
  4. Quick Check:

    Reverse order uses b <=> a [OK]
Hint: Reverse order: swap a and b in <=> [OK]
Common Mistakes:
  • Using a <=> b for descending sort
  • Misusing sort_by with spaceship operator
  • Returning boolean instead of integer in block
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard