> ## Content Index
> Fetch the complete content index at: https://www.modernlearner.org/llms.txt
> Use this file to discover other available public pages before exploring further.

# Checking Equality of Objects in Ruby
- URL: https://www.modernlearner.org/checking-equality-of-objects-in-ruby/
- Published: 2020-10-19T15:29:53.000Z
- Updated: 2024-04-09T22:25:49.000Z
- Author: Modern Learner

There are four methods for checking equality of objects in the Ruby programming language:

- `===` used for case comparison
- `==` used for equality of objects
- `eql?` used for determining object identity
- `equal` used for comparing the hash key of two objects

# Case Equality with `===`

The [\=== method is used for checking the case equality of two objects](https://ruby-doc.org/core-2.7.1/Object.html?ref=modernlearner.org#method-i-3D-3D-3D). When you use the `case` statement, this method will be the one used to see whether or not there is a match.

Here's an example. We have a class `Book` where we are storing a unique id, the `isbn`. When two books are compared, they are equal if their ISBNs are the same.

```ruby
class Book
  attr_accessor :isbn
  
  def initialize(isbn)
    @isbn = isbn
  end

  def ===(other)
    isbn == other.isbn
  end
end

bookA = Book.new(1000)
bookB = Book.new(2000)

puts bookA === bookA # => true
puts bookA === Book.new(1000) # => false
puts bookB === bookA # => false
puts bookB === bookB # => true
puts bookB === Book.new(1000) # => false
puts bookB === Book.new(2000) # => true

puts case Book.new(2000)
  when bookA
    'ISBN matches book A'
  when bookB
    'ISBN matches book b'
  else
    'no match'
end # => ISBN matche book b

```

If we did *not* override the `===` equality method, the default comparison would have used the `==` method.

Let's consider another example: a user session for [product analytics](https://www.modernlearner.org/product-analytics-introduction/). The class has a field for the user id and the session start time. We can define the equality to be a comparison between both fields. Since only comparing the user ids would result in all sessions from one user to be seen as equivalent, both fields must be compared.

```ruby
class UserSession
  attr_accessor :user_id, :session_start_time
  
  def ==(other)
    self.user_id == other.user_id && self.session_start_time == other.session_start_time
  end
end

```

# `==` equality comparison

The [\== method is typically overridden by classes to provide class-specific meaning to the equality comparison](https://ruby-doc.org/core-2.7.1/BasicObject.html?ref=modernlearner.org#method-i-3D-3D).

This is similar to what we did above in the `Book` code example:

```ruby
class Book
  attr_accessor :title
  
  def initialize(title)
    @title = title
  end

  def ==(other)
    title.downcase == other.title.downcase
  end
end

```

# `eql?` comparison

The [eql? method](https://ruby-doc.org/core-2.7.1/Object.html?ref=modernlearner.org#method-i-eql-3F) compares the [object hash key](https://ruby-doc.org/core-2.7.1/Object.html?ref=modernlearner.org#method-i-hash).

For example:

```ruby
value = {'a': 1}

other = {'a': 1}
puts value == other # => true
puts value.eql?(other) # => true

other = value
puts value == other # => true
puts value.eql?(other) # => true

```

# `equal?` comparison

In contrast to `eql?`, the `equal?` method compares the object *identity*.

For example:

```ruby
value = {'a': 1}

other = {'a': 1}
puts value == other # => true
puts value.equal?(other) # => false

other = value
puts value == other # => true
puts value.equal?(other) # => true

```