This blog is part of our Ruby 2.4 series.
Object#equal? method is used to compare two objects by their
identity, that is, the two objects are exactly the same or not.
Ruby also has
Object#eql? method which returns true if two objects
have the same value.
We can see that object ids of the objects are not same.
In ruby, Set
does not allow duplicate items in its collection.
To determine if two items are equal or not in a
Object#eql? and not
So if we want to add two different objects with the same values in a set, that would not have been possible prior to Ruby 2.4 .
But with the new
Set#compare_by_identity method introduced in Ruby 2.4,
sets can now compare its values using
and check for the exact same objects.
Ruby 2.4 also provides the compare_by_identity? method to know if the set will compare its elements by their identity.