Ruby Array () function with example
Last Updated :
08 Jan, 2020
Array#() : () is an Array class method which performs the comparison between the two arrays.
Syntax: Array.()
Parameter: Array for the comparison
Return: -1 : array is less than the other array
0 : array is equal to other array
1 : array is greater than the other array
Example #1 :
a = [ 18 , 22 , 33 , 4 , 5 , 6 ]
b = [ 5 , 4 , 22 , 1 , 88 , 9 ]
c = [ 18 , 22 , 33 , 40 , 50 , 6 ]
puts "comparison : #{a <=> b}\n\n"
puts "comparison : #{a <=> c}\n\n"
puts "comparison : #{b <=> c}\n\n"
|
Output :
comparison : 1
comparison : -1
comparison : -1
Example #2 :
a = [ "abc" , "xyz" , "dog" ]
b = [ "cow" , "cat" , "dog" ]
c = [ "cat" , "1" , "dog" ]
puts "comparison : #{a <=> b}\n\n"
puts "comparison : #{a <=> c}\n\n"
puts "comparison : #{b <=> c}\n\n"
|
Output :
comparison : -1
comparison : -1
comparison : 1
Share your thoughts in the comments
Please Login to comment...