0
0
Rubyprogramming~10 mins

Spaceship operator (<=>) in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compare two numbers using the spaceship operator.

Ruby
result = 5 [1] 3
Drag options to blanks, or click blank then click option'
A<=>
B==
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of the spaceship operator.
Using > or < which only return boolean values.
2fill in blank
medium

Complete the code to compare two strings alphabetically using the spaceship operator.

Ruby
result = 'apple' [1] 'banana'
Drag options to blanks, or click blank then click option'
A<
B==
C>=
D<=>
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which only checks equality.
Using < which returns boolean, not -1, 0, or 1.
3fill in blank
hard

Fix the error in the code by replacing the incorrect operator with the spaceship operator.

Ruby
result = 10 [1] 10
Drag options to blanks, or click blank then click option'
A==
B<=>
C=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which returns boolean true or false.
Using = which is assignment, not comparison.
4fill in blank
hard

Fill both blanks to create a hash that maps numbers to their comparison results with 7 using the spaceship operator.

Ruby
comparisons = Hash[[5, 7, 9].map { |num| [num, num [1] 7] if num [2] 7 }.compact]
Drag options to blanks, or click blank then click option'
A<=>
B==
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of <=> in the hash values.
Using == or <=> in the filter condition incorrectly.
5fill in blank
hard

Fill all three blanks to create a hash that maps words to their length comparison with 4 using the spaceship operator and a condition.

Ruby
result = Hash[['cat', 'house', 'dog'].map { |word| [word, word.length [1] 4] if word.length [2] 4 and word [3] 'dog' }.compact]
Drag options to blanks, or click blank then click option'
A<=>
B>
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of <=> for length comparison.
Using == instead of != to exclude 'dog'.