0020 Equal

Why so many methods of comparison?

  • equal? corresponds to instance equality. Is it the same object in memory?
  • == and eql? are in most cases aliases. They correspond to the « natural » equality of object values (Exception for Object: alias for equal?).
  • === is called « case equality ». It is this method that is called in a case...when. By default, it is an alias of ==.

0020-equal_1.png

The === can be seen as a notion of belonging: do you belong to what I represent? ⚠️ This method is generally neither reflexive, symmetrical nor transitive.

0020-equal_2.png

The documentation states: « The eql? method returns true if obj and other refer to the same hash key ». In practice, this often translates into object type equality. In a way, eql? is the equivalent of Javascript’s === (and not Ruby’s ===!).

0020-equal_3.png

⚠️ Counter-examples can be found on some implementations of the eql? method that don’t respect the definition.

0020-equal_4.png

Links to Ruby documentation for ==, eql?, equal? and ===.