Home » Ruby programming

Comparing Array instances with <=> in Ruby

Here, we are going to learn how to compare Array instances with <=> in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on January 06, 2020

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.

Method description

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.

Case 1: 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.

Case 2: 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.

Case 3: 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.

Case 4: 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.



Comments and Discussions!

Load comments ↻





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