---
title: "Ruby 2.4 adds compare by identity functionality"
description:
  "Ruby 2.4 adds compare_by_identity and compare_by_identity? methods for Set
  class so that now we can compare the set elements by their identitiy rather
  than value"
canonical_url: "https://www.bigbinary.com/blog/ruby-2-4-adds-compare-by-identity-functionality-for-sets"
markdown_url: "https://www.bigbinary.com/blog/ruby-2-4-adds-compare-by-identity-functionality-for-sets.md"
---

# Ruby 2.4 adds compare by identity functionality

Ruby 2.4 adds compare_by_identity and compare_by_identity? methods for Set class
so that now we can compare the set elements by their identitiy rather than value

- Author: Chirag Shah
- Published: December 29, 2016
- Categories: Ruby 2.4, Ruby

In Ruby, `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.

For example:

```ruby

str1 = "Sample string"
str2 = str1.dup

str1.eql?(str2) #=> true

str1.equal?(str2) #=> false

```

We can see that object ids of the objects are not same.

```ruby

str1.object_id #=> 70334175057920

str2.object_id #=> 70334195702480

```

In ruby, [Set](http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html) does
not allow duplicate items in its collection. To determine if two items are equal
or not in a `Set` ruby uses `Object#eql?` and not `Object#equal?`.

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 .

### Ruby 2.3

```ruby

require 'set'

set = Set.new #=> #<Set: {}>

str1 = "Sample string" #=> "Sample string"
str2 = str1.dup #=> "Sample string"

set.add(str1) #=> #<Set: {"Sample string"}>
set.add(str2) #=> #<Set: {"Sample string"}>

```

But with the new
[Set#compare_by_identity method introduced in Ruby 2.4](http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-compare_by_identity),
sets can now compare its values using `Object#equal?` and check for the exact
same objects.

### Ruby 2.4

```ruby

require 'set'

set = Set.new.compare_by_identity #=> #<Set: {}>

str1 = "Sample string" #=> "Sample string"
str2 = str1.dup #=> "Sample string"

set.add(str1) #=> #<Set: {"Sample string"}>
set.add(str2) #=> #<Set: {"Sample string", "Sample string"}>

```

## Set#compare_by_identity?

Ruby 2.4 also provides the
[compare_by_identity? method](http://ruby-doc.org/stdlib-2.4.0/libdoc/set/rdoc/Set.html#method-i-compare_by_identity-3F)
to know if the set will compare its elements by their identity.

```ruby

require 'set'

set1= Set.new #=> #<Set: {}>
set2= Set.new.compare_by_identity #=> #<Set: {}>

set1.compare_by_identity? #=> false

set2.compare_by_identity? #=> true

```

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2-4-adds-compare-by-identity-functionality-for-sets)
