Ruby Array () function with example
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 :
# Ruby code for <=>() method # showing comparison # declaring array a = [18, 22, 33, 4, 5, 6] # declaring array b = [5, 4, 22, 1, 88, 9] # declaring array c = [18, 22, 33, 40, 50, 6] # 1 as a>b puts "comparison : #{a <=> b}\n\n" # -1 as a<c puts "comparison : #{a <=> c}\n\n" # -1 as b<c puts "comparison : #{b <=> c}\n\n" |
chevron_right
filter_none
Output :
comparison : 1 comparison : -1 comparison : -1
Example #2 :
# Ruby code for <=>() method # showing comparison # declaring array a = ["abc", "xyz", "dog"] # declaring array b = ["cow", "cat", "dog"] # declaring array c = ["cat", "1", "dog"] # -1 as a<b puts "comparison : #{a <=> b}\n\n" # -1 as a<c puts "comparison : #{a <=> c}\n\n" # 1 as b>c puts "comparison : #{b <=> c}\n\n" |
chevron_right
filter_none
Output :
comparison : -1 comparison : -1 comparison : 1
Recommended Posts:
- Ruby | Array none?() function
- Ruby | Array take() function
- Ruby | Array map() function
- Ruby | Array <=> function
- Ruby | Array &() function
- Ruby | Array max() function
- Ruby | Array one?() function
- Ruby | Array zip() function
- Ruby | Array min() function
- Ruby | Array pop() function
- Ruby | Array rotate!() function
- Ruby | Array sort() function
- Ruby | Array rotate() function
- Ruby | Array abbrev() function
- Ruby | Array sample() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


