Monday, May 14, 2007

Ruby Equality

Since two people asked me in the space of 5 days about this, I thought I'd post a quick demystification of Ruby's "equality" facilities. Namely equal?, ==, eql? and ===.


  1. equal? - always (or should be) purely identity equality, and *should never* be overridden by subclasses

  2. == - by default is object identity equality, but *can* be overridden by subclasses

  3. eql? - is object value equality and *should* be overridden by subclasses

  4. === - by default same as #==. It is overridden by subclasses that need a "case" equality defined to make sense when used in the case statements in a when construct.

0 comments: