Ruby Equality

Written on 12:48:00 PM by S. Potter

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.

If you enjoyed this post Subscribe to our feed

No Comment

Post a Comment