Apart from simple comparision with == or eql? there is a another one: Object#=== aka Threequal.
Its intention is to provide “Case Equality”. The Documention furthermore states:
For class Object, effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements.
It is meant to to be overwritten!
In some classes it is already achieved. For example Range#===:
(1..5) === 3 # => true
So there is a comparision going on between two kind of objects, a Range and a Fixnum. Briefly the Threequal operator technically is nothing more than a method expecting a parameter, similar to == or +.
But this one is special. The Threequal also is called internally when a case statement tries to compare. The Range class once again, evaluating the a certain body mass index (BMI):
case 24 when 0..18 'Underweight' when 18..25 'Normal weight' else 'Adiposity (overweight)' end # => 'Normal weight'
Please note, the Threequal has to be overwritten in the when-branches-object class, not in the one the case statement tries to compare.
So whenever semantic comparision is required overwriting Object#=== totally makes sense. Semantic comparision is emphasized.
Another scenario is teams having members. The Team class:
class Team def initialize *names @members = names end def === name @members.include? name end end
The team Threequal method verifies, if its members list contain the assigned name. Creating 2 teams:
sales = Team.new 'Alice', 'Bob' development = Team.new 'Chris', 'Deborah'
Given there is no other option then using a case statement for verifying the team, Chris belongs to, it would probably look like:
case 'Chris' when sales then 'Sales' when development then 'Development' else 'None' end # => "Development"
And it returns the right answer.