

Idiosyncratic Ruby: Struggling Four Equality
source link: https://idiosyncratic-ruby.com/55-struggling-four-equality.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Another of Ruby's idiosyncrasies 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
Implicitly 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 implicitly 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
- Define a meaningful
==
which returnstrue
if two objects should be considered the same - Make
eql?
return the same value==
, but also limit it to return onlytrue
if both object are instances of the same class - Don't redefine
equal?
- Be creative with
===
(On a reasonable level. Other people using your code expect it to be some kind of useful relationship check)
Also See
More Idiosyncratic Ruby
Recommend
-
16
Ruby TRICKS of 2018 Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of P...
-
10
Nothing to Disable Ruby's mode of operation can be altered with some --enable-* / --disable-* command-line switches....
-
17
Super Snakes Have you ever been confused by the __underscores__ required by some of __RUBY__'s features? You can get i...
-
8
The Ruby core team cares a lot about Unicode, and this is why we have pretty good Unicode support in the language. Even though the Unicode standard evolves consta...
-
13
Clear Case of Unclear Casing Recent Ruby versions allow you to choose from a wide-range of uppercase letters - beyond just ASCII - to st...
-
10
Nothing to Escape What is your wild guess: How many different ways does Ruby provide for inserting a NULL byte into a double-quote...
-
11
How does nothing (as in nil, null, or nan) compare to nothing? Equality Equality ==...
-
11
Assignments In-Style The introduction of pattern matching in Ruby 2.7 brought us a new style of multi-assigning local variables: The
-
9
Warning: The Experiment Ruby's Warning module learned some new tricks in...
-
10
Ruby comes with good support for Unicode-related features. Read on if you want to learn more about important Unicode fundamentals and how to use them in Ruby… …or just watch my talk from RubyConf 2017:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK