Home » Ruby programming

Finding whether two arrays are identical or not in Ruby

Here, we are going to learn how to find/check whether two arrays are identical or not in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on January 06, 2020

In the last article, we have seen how we can implement <=> operator to find the difference between two Array instances?

The method was giving different results for different scenarios like -1 if the first Array instance is less than the second Array instance. We can verify two Array instances are identical or not with the help of <=> also as when this operator throws 0 as the result, it means that two Array instances are the same. In the article, we will see two more ways through which we can find alternative solutions to the situation and the situation is, as mentioned Finding two instances of Array class are same or not.

When two objects of Array class are the same, this means that they both are of the same length and contain the same element values. This can be checked with the help of == operator as well as with the help of .eql? method. Well, they both are Public instance method living inside Ruby library. Both the ways will give you a response in Boolean value, they will give true if the Arrays are equal and false when the Array instances are not identical. Let us see their syntaxes and examples for understanding them in a better manner.

Method 1: Checking with the help of == method.

Syntax:

    array1 == array2

Example:

=begin
  Ruby program to demonstrate == operator
=end

# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]

# comparing
rslt = array1 == array2

# printing the result
if rslt == true
	puts "Both the Array instances are identical"
else
	puts "Array instances are not identical"
end

Output

Both the Array instances are identical

Explanation:

In the above code, you can observe that we have created two Array instance. We have checked their equality with the help of == operator. We have stored the Boolean result inside a variable and as a result, we got that both the Array instances are identical as the method has returned "true".

Method 2: Checking with the help of .eql? method.

Syntax:

    array1.eql?(array2)

Example:

=begin
  Ruby program to demonstrate .eql? method
=end

# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = [1,"Ramesh","Apple",12,true,0,"Satyam","Harish"]

# comparing
rslt = array1.eql?(array2)

# printing the result
if rslt == true
	puts "Both the Array instances are identical"
else
	puts "Array instances are not identical"
end

Output

Array instances are not identical

Explanation:

In the above code, you can observe that we have created two Array instance. We have checked their equality with the help of .eql? method. We have stored the Boolean result inside a variable and as a result, we got that both the Array instances are not identical as the method has returned "false".



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.