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))
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)
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))
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.