Search code examples
rubyequalitycommutativity

3 Equals or Case Equality operator


In Ruby Integer === 5 returns true. Similarly String === "karthik" returns true.
However, 5 === Integer returns false. And "karthik" === String.
Why is the operator not commutative?


Solution

  • The simple answer is: because it doesn't make sense. The relationship the operator describes is not commutative, why should the operator be?

    Just look at your own examples: 5 is an Integer. But is Integer a 5? What does that even mean?

    === is the case subsumption operator, and subsumption doesn't commute.

    The fact that the case subsumption operator uses equals signs, and that it is usually called the triple equals, threequals or case equality operator is terribly unfortunate since it not only has absolutely nothing to do with equality, but it also does not conform to many of the laws that equality conforms to, such as transitivity and as you mentioned commutativity.

    For more of my ranting about === see