Ruby | Array <=> function
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 :
# Ruby code for <=>() method # checking equality # declaring arrays a = [18, 22, 33, 4, 5, 6] # declaring arrays b = [18, 22, 33, 4, 5, 6] # declaring arrays c = [18, 22, 33, 40, 50, 6] # <=> method puts "<=> method : #{a <=> b}\n\n" # <=> method puts "<=> method : #{a <=> c}\n\n" # <=> method puts "<=> method : #{b <=> c}\n\n" |
chevron_right
filter_none
Output :
method : 0 method : -1 method : -1
Example #2 :
# Ruby code for <=>() method # checking equality # declaring arrays a = ["abc", "xyz", "dog"] # declaring arrays b = ["cat", "cat", "dog"] # declaring arrays c = ["cat", "cat", "dog"] # <=> method puts "<=> method : #{a <=> b}\n\n" # <=> method puts "<=> method : #{a <=> c}\n\n" # <=> method puts "<=> method : #{b <=> c}\n\n" |
chevron_right
filter_none
Output :
method : -1 method : -1 method : 0
Recommended Posts:
- Ruby | Array map() function
- Ruby | Array take() function
- Ruby | Array &() function
- Ruby | Array none?() function
- Ruby | Array min() function
- Ruby | Array max() function
- Ruby | Array pop() function
- Ruby Array () function with example
- Ruby | Array one?() function
- Ruby | Array zip() function
- Ruby | Array select!() function
- Ruby | Array sort_by!() function
- Ruby | Array select() function
- Ruby | Array pack() function
- Ruby | Array reverse_each() 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.

