Using the Spaceship Operator (<=>) in Ruby
📖 Scenario: You are working on a simple ranking system for a game. You want to compare players' scores to see who is higher, lower, or if they are equal.
🎯 Goal: Build a Ruby program that uses the spaceship operator () to compare two player scores and print the comparison result.
📋 What You'll Learn
Create two integer variables named
score1 and score2 with exact values 85 and 90 respectively.Create a variable called
comparison that stores the result of score1 <=> score2.Use a
case statement with comparison to print "score1 is less than score2", "score1 is equal to score2", or "score1 is greater than score2" based on the comparison result.Print the final message to the console.
💡 Why This Matters
🌍 Real World
Comparing scores or rankings is common in games, leaderboards, and sorting lists.
💼 Career
Understanding comparison operators like the spaceship operator helps in writing clean and efficient sorting and comparison logic in software development.
Progress0 / 4 steps