Table of contents
1.
Introduction
2.
Object Equality
2.1.
The "equal?" Method
2.1.1.
Example
2.1.2.
Output
2.2.
The "==" Operator
2.2.1.
Example
2.2.2.
Output
2.3.
The "===" Operator
2.3.1.
Example
2.3.2.
Output
2.4.
The "eql?" Method
2.4.1.
Example
2.4.2.
Output
3.
Frequently Asked Questions
3.1.
What is a class in Ruby?
3.2.
How to define a class in Ruby?
3.3.
What is an object in Ruby?
3.4.
How to create an object in Ruby?
4.
Conclusion
Last Updated: Mar 27, 2024

Object Equality in Ruby

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello and Welcome, readers! We hope you are doing great.

Ruby is an open-source, high-level, general-purpose programming language designed by Yukihiro Matsumoto in the mid-1990s. The focus of developing this programming language was on simplicity and programming productivity

To learn about Ruby, check out our articles on Ruby.

Today, In this blog, we will discuss Object Equality in Ruby with proper explanation and implementation. You will get a clear idea about the object equality operations from this article, so follow the article till the end.

So, without further ado, let’s start our discussion.

Object Equality

In Ruby, there are different ways by which we can compare the equality of objects. These are as follows: The "equal?" method, The "==" operator, The "===" operator, and The "eql?" method.

Let’s now explain each of these ways separately.

The "equal?" Method

The "equal?" method checks whether or not two values refer to the same object. It returns the followings:

  • If two values refer to the same object, it returns true.
  • If two values refer to two distinct objects, it returns false.

Example

# x referring to one String object
x = "Coding Ninjas"

# y and z referring to another String object
y = z = "Coding Ninjas"

# x and y referring to two distinct objects
puts("Object Equality for x and y:")
puts(x.equal?(y))
# y and z referring to the same object
puts("Object Equality for y and z:")
puts(y.equal?(z))
You can also try this code with Online Ruby Compiler
Run Code

Output

Object Equality for x and y:
false
Object Equality for y and z:
true

The "==" Operator

The "==" operator is used to test whether two object references are identical or not. It is the most common way to check for equality. Most of the standard classes in ruby, like Array and Hash classes, define this operator to check for equality.

According to the "==" operator, two arrays are equal if and only if they have the same number of elements and if their corresponding elements are also equal. 

Similarly, Two hashes are equal according to the "==" operator if and only if they contain the same number of key-value pairs and the keys and values are themselves equal.

Example

x = "Coding Ninjas"
y = "Coding Ninjas"
z = "Hello World"

puts("Object Equality for x and y:")
puts(x == y)

puts("Object Equality for y and z:")
puts(y == z)
You can also try this code with Online Ruby Compiler
Run Code

Output

Object Equality for x and y:
true
Object Equality for y and z:
false

The "===" Operator

In Ruby, the "===" operator, also known as the "case equality" operator, is used to check equality for a target value of a case statement within a "when" clause of that case statement.

In most classes, the "===" operator is the same as the "==" operator. But certain classes define the "===" operator differently, like:

  • The Range class defines the "===" operator to check whether a value is in the range or not.
  • The Symbol class defines the "===" operator to check whether the right-hand operator is the same symbol as the left hand or whether the string holds the same text.
  • The Regexp class defines the "===" operator to check whether a string matches the regular expression.

The operator is more of a matching or membership operator for the above classes.

Example

# range
x = 1..10
y = 5
puts("Object Equality for x and y:")
puts(x === y)

#regexp
a = /\d+/
b = "123456789"
puts("Object Equality for a and b:")
puts(a === b)

Output

Object Equality for x and y:
true
Object Equality for a and b:
true

The "eql?" Method

In Ruby, the "eql?" method is a synonym for the "equal?" method. Most of the classes that override it use it as a strict version of the "==" operator as it does no type conversion. 

Example

# First check the equality using the "==" operator
x = 5
y = 5.0
puts("Object Equality for x and y(x == y):")
puts(x == y)

# Now check the equality using the "eql?" method
puts("Object Equality for x and y(x.eql?(y)):")
puts(x.eql?(y))
You can also try this code with Online Ruby Compiler
Run Code

Output

Object Equality for x and y(x == y):
true
Object Equality for x and y(x.eql?(y)):
false

 

Read about Bitwise Operators in C here.

Frequently Asked Questions

What is a class in Ruby?

In Ruby, a class is a user-defined template consisting of methods and variables. It is the blueprint from which different objects are created.
 

How to define a class in Ruby?

In Ruby, a class always starts with the keyword "class", followed by the name of the class. The first letter of the name should be in the capital. The class is terminated by the "end" keyword.

For example, below is the definition of a class,

class Ninjas
   # ... class definition
end
You can also try this code with Online Ruby Compiler
Run Code


What is an object in Ruby?

In Ruby, the objects are the instances of a class.
 

How to create an object in Ruby?

In Ruby, we can create an object of a class using the method "new" of a class.

For example, to create an object of the class "Ninjas", we do the following:

Ninjas_Object = Ninjas.new
You can also try this code with Online Ruby Compiler
Run Code

Conclusion

In this article, we have extensively discussed Object Equality in Ruby.

We started with the basic introduction. Then we discussed the followings:

  • The "equal?" method
  • The "==" operator
  • The "===" operator
  • The "eql?" method
     

We hope this blog gives you some ideas regarding Object Equality in Ruby. If you would like to learn more, follow our articles on Bitwise NOT(~), AND(&), OR(|) and XOR(^) Operations in Ruby8 Reasons Why Ruby Should be Your First Language, Ruby and Ruby On Rails: How do they differ?Methods in Ruby and Nonoperators in RubyExplore our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!

Do upvote this blog to help other ninjas grow.

Happy Reading!

Live masterclass