Recall & Review
beginner
What does the spaceship operator (<=>) do in Ruby?
It compares two values and returns -1 if the left is less, 0 if equal, and 1 if the left is greater.
Click to reveal answer
beginner
What is the return value of 5 <=> 10 in Ruby?
It returns -1 because 5 is less than 10.
Click to reveal answer
intermediate
How can the spaceship operator help in sorting arrays?
It provides a simple way to define custom comparison logic for sorting by returning -1, 0, or 1.
Click to reveal answer
beginner
What will be the result of 'apple' <=> 'banana' in Ruby?
It returns -1 because 'apple' comes before 'banana' alphabetically.
Click to reveal answer
advanced
Can the spaceship operator be used with custom objects in Ruby?
Yes, by defining the <=> method inside the object's class to specify how to compare instances.Click to reveal answer
What does the spaceship operator (<=>) return when the left value is greater than the right?
✗ Incorrect
It returns 1 when the left value is greater than the right.
Which of these is a valid use of the spaceship operator in Ruby?
✗ Incorrect
Only '5 <=> 3' is the correct syntax for the spaceship operator.
What does the spaceship operator return if two values are equal?
✗ Incorrect
It returns 0 when both values are equal.
How can the spaceship operator be used in sorting an array?
✗ Incorrect
You use <=> inside a block to tell Ruby how to compare elements during sorting.
What happens if you use the spaceship operator on objects without defining <=> method?
✗ Incorrect
If <=> is not defined for objects, it returns nil indicating they can't be compared.
Explain how the spaceship operator (<=>) works in Ruby and give an example.
Think about how it tells if one value is less, equal, or greater than another.
You got /3 concepts.
Describe how you can use the spaceship operator to sort an array with custom objects.
Custom objects need a way to compare themselves for sorting.
You got /3 concepts.