Struggling Four Equality

Another of Ruby's idiosycrasies is equalness. It's not too complicated, but naming is an issue here.

Four Concepts of Equalness

equal? Object Identity Comparison

This one is easy. Two objects should be considered identical. Think: x.object_id == y.object_id

== Equality Equality

This is the usual method to care about. Two objects should be treated the same. If the class supports the <=> spaceship comparison operator, it is expected that == returns true for the same values, <=> returns 0 for.

eql? Hash Key Equality

Normally, this is the same as ==: "…eql? is usually aliased to the overridden == method"

The most important effect of the result of eql? is to distinguish between hash keys: "Two objects refer to the same hash key when their hash value is identical and the two objects are eql? to each other". A real life example:

1 == 1.0 # => true
1.eql?(1.0) # => false

# this means that the following will be treated as two different keys
{1: "Idiosyncratic", 1.0 "Hash"}

So eql? is a little stricter than ==, because it will return false if two objects are not instances of the same class. A typical implementation looks like this:

def eql?(other)
  instance_of?(other.class) && self == other
end

=== Fancy Equality

Implicitely used for case statements. Usually like ==, but can also mean that something has some kind of relationship, like being some kind of a class.

Equality Implementations for Core Classes

Class eql? == ===
Object Identity (like equal?) Same as eql? Same as ==
Symbol - - -
Numeric Same type, same value Same value, according to spaceship returning 0 -
String Same length, same contents If other is a String: eql?, else: other.to_str === self -
Regexp If other is a Regexp: Same pattern, same options, same encoding Same as eql? If other is a String: Match against self
Array Same length, every element .eql? corresponding other element Same length, every element == corresponding other element. Will implicetly convert other object via .to_ary -
Hash Same length, every element == corresponding other element (order not relevant) Same as eql? -
Module - - other.is_a?(self)
Class - - other.is_a?(self)

Meaning of -: Not defined / Use Object's implementation

Best Practices for Sub Classes

Also See

More Idiosyncratic Ruby