Alex Rivera | Logout

3 Equals or Case Equality operator

Asked 2010-12-24T16:11:09.343
23

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?

Edit
Report

1 Answer

1

it needs to implement case-when comparisons

It's normal to have non-commutative operators.

/ - % [] . -> ^ << >> < <= > >= && || = += -= ,

And as it happens, === exists in part as the case-when operator. That's rather elaborate in Ruby, and it couldn't be so if it had to be simplified to a commutative op.

answered 2010-12-24T17:56:20.207

Your Answer