Home »
Ruby Tutorial
Ruby - Comparing Array instances
By IncludeHelp Last updated : November 22, 2024
In the last article, we have seen how one can add an object into an Array element with the help of << operator? That was a Public instance method. In this article, we will see how we can compare two Array instances with the help of <=> operator? This operator is also one of the examples of Public instance methods. Let us see how we can carry out this comparison.
Comparing Array instances using <=> method
In the case of this method, the comparison is carried out in an element-wise manner. Sometimes, the result is dependent upon the comparison of the first elements of corresponding Array instances only. This method works in a way that each object of each Array is compared with the help of <=> operator. The first two elements that are not identical to each other determine the result of the whole comparison. Now, we will be studying cases that will determine the result of the comparison being carried out between two instances of Array class.
When Array is lesser than other Array
The return type of this method is an integer and it will return -1 when the first Array instance is lesser than another Array instance. Let us understand this with the help of an example and observe the result of the comparison.
Example
=begin
Ruby program to demonstrate <=>
=end
# arrays
first_array = ["a","a","c"]
second_array = ["a","b","c"]
# comparing
rslt = first_array <=> second_array
# printing the result
puts "The result of comparison is #{rslt}"
Output
The result of comparison is -1
Explanation
You can observe that the result of the comparison is -1 because, in both the arrays, differences are coming at the second index. The object "a" is smaller than the object "b", that is why we are getting -1 as the result of the comparison.
When Array is greater than other Array
The return type of this method is an integer and it will return 1 when the first Array instance is greater than another Array instance. Let us understand this with the help of an example and observe the result of the comparison.
Example
=begin
Ruby program to demonstrate <=>
=end
# arrays
first_array = ["a","c","c"]
second_array = ["a","a","c"]
# comparing
rslt = first_array <=> second_array
# printing the result
puts "The result of comparison is #{rslt}"
Output
The result of comparison is 1
Explanation
You can observe that the result of the comparison is 1 because, in both the arrays, differences are coming at the second index. The object "c" is greater than the object "a", that is why we are getting 1 as the result of the comparison.
When both the Array instances are equal
When both the array instances are equal then this method will return 0. Two Array instances are called to be equal if and only if the values of both the Arrays are the same and the length of the first Array is equal to the length of another Array. Let us understand this with the help of an example and observe the result of the comparison.
Example
=begin
Ruby program to demonstrate <=>
=end
# arrays
first_array = ["a","c","c"]
second_array = ["a","c","c"]
# comparing
rslt = first_array <=> second_array
# printing the result
puts "The result of comparison is #{rslt}"
Output
The result of comparison is 0
Explanation
In the above code, you can observe that both the Arrays are exactly the same in length as well as in values that is why we are getting 0 as the result of the comparison.
When two values are incomparable
You will get 'nil' as the result of comparison when two values are incomparable to each other. Let us understand this with the help of an example and observe the output.
Example
=begin
Ruby program to demonstrate <=>
=end
# arrays
first_array = ["a","c","c"]
second_array = [1,2,3]
# comparing
rslt = first_array <=> second_array
# printing the result
puts "The result of comparison is #{rslt}"
Output
The result of comparison is
Explanation
In the above code, you can observe that we are getting 'nil' as the result of comparison because integer value can't be compared with character value.