0
0
Rubyprogramming~10 mins

Comparable module usage in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Comparable module usage
Define class with <=> method
Include Comparable module
Create objects
Use comparison operators (>, <, ==, etc.)
Ruby calls <=> method internally
Return true/false based on <=> result
Comparison result used in code
The class defines a <=> method and includes Comparable. Ruby uses <=> to compare objects with operators like >, <, ==.
Execution Sample
Ruby
class Box
  include Comparable
  attr_reader :volume
  def initialize(volume)
    @volume = volume
  end
  def <=>(other)
    volume <=> other.volume
  end
end
b1 = Box.new(10)
b2 = Box.new(20)
p b1 < b2
This code compares two Box objects by their volume using Comparable.
Execution Table
StepActionEvaluationResult
1Create b1 with volume 10b1.volume = 10b1 object created
2Create b2 with volume 20b2.volume = 20b2 object created
3Compare b1 < b2Calls b1.<=>(b2)10 <=> 20 returns -1
4Interpret <=> result-1 means b1 < b2 is trueComparison returns true
5Print resultp truetrue printed
💡 Comparison completes after <=> returns -1, so b1 < b2 is true
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
b1.volumeundefined10101010
b2.volumeundefinedundefined202020
comparison_resultundefinedundefinedundefined-1-1
Key Moments - 3 Insights
Why does Ruby use the <=> method when we write b1 < b2?
Ruby calls the <=> method internally to decide the result of <, >, == operators as shown in execution_table step 3.
What happens if the <=> method returns nil?
If <=> returns nil, Ruby cannot compare objects and raises an error or returns false for comparisons, because nil means no comparison possible.
Why do we include Comparable module in the class?
Including Comparable gives us all comparison operators automatically, but it requires defining <=> method to work, as shown in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of comparison_result after step 3?
A1
B-1
C0
Dnil
💡 Hint
Check the 'Evaluation' and 'Result' columns in step 3 of execution_table.
At which step does Ruby decide the comparison b1 < b2 is true?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Interpret <=> result' action in execution_table step 4.
If the <=> method returned 0, what would be the result of b1 < b2?
Afalse
Btrue
Cnil
Derror
💡 Hint
Recall that 0 means objects are equal, so b1 < b2 would be false.
Concept Snapshot
Define <=> method in your class
Include Comparable module
Use <, >, == operators on objects
Ruby calls <=> internally
Return -1, 0, 1 from <=> for comparison
Nil means no comparison possible
Full Transcript
This example shows how to use the Comparable module in Ruby. First, you define a class with a <=> method that compares two objects. Then you include the Comparable module to get comparison operators like < and >. When you compare two objects, Ruby calls the <=> method internally. The <=> method returns -1 if the first object is less, 0 if equal, and 1 if greater. This return value determines the result of the comparison operators. If <=> returns nil, Ruby cannot compare the objects. The execution table traces creating two Box objects with volumes 10 and 20, then comparing them with <. The comparison calls <=> which returns -1, so b1 < b2 is true. This visual helps understand how Comparable works step-by-step.