Ruby program to compare two arrays using the eql?() method

Ruby Example: Write a program to compare two arrays using the eql?() method.
Submitted by Nidhi, on January 14, 2022

Problem Solution:

In this program, we will create three arrays of integers then we will compare arrays using the eql?() method and print the appropriate message.

Program/Source Code:

The source code to compare two arrays using the eql?() method is given below. The given program is compiled and executed successfully.

# Ruby program to compare two arrays 
# using eql?() method

arr1 = [10,20,30,20,50,30];
arr2 = [10,50,30,40,60,70];
arr3 = [10,20,30,20,50,30];

if arr1.eql?(arr2)
    print "arr1 and arr2 are equal.\n";
else
    print "arr1 and arr2 are not equal.\n";
end

if arr1.eql?(arr3)
    print "arr1 and arr3 are equal.\n";
else
    print "arr1 and arr3 are not equal.\n";
end

Output:

arr1 and arr2 are not equal.
arr1 and arr3 are equal.

Explanation:

In the above program, we created 3 arrays arr1, arr2, arr3 with some elements. Then we compared arrays using the eql?() method and printed the appropriate message.

Ruby Arrays Programs »




Comments and Discussions!

Load comments ↻





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