Ruby | Array <=> function
Last Updated :
05 Dec, 2019
Array#() : () is an Array class method which performs the comparison between the two arrays.
Syntax: Array.()
Parameter: Array for the comparison
Return: 1 : if a > b
-1 : if a < b
0 : if a = b
Example #1 :
a = [ 18 , 22 , 33 , 4 , 5 , 6 ]
b = [ 18 , 22 , 33 , 4 , 5 , 6 ]
c = [ 18 , 22 , 33 , 40 , 50 , 6 ]
puts "<=> method : #{a <=> b}\n\n"
puts "<=> method : #{a <=> c}\n\n"
puts "<=> method : #{b <=> c}\n\n"
|
Output :
method : 0
method : -1
method : -1
Example #2 :
a = [ "abc" , "xyz" , "dog" ]
b = [ "cat" , "cat" , "dog" ]
c = [ "cat" , "cat" , "dog" ]
puts "<=> method : #{a <=> b}\n\n"
puts "<=> method : #{a <=> c}\n\n"
puts "<=> method : #{b <=> c}\n\n"
|
Output :
method : -1
method : -1
method : 0
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...