Open In App

Ruby | Array <=> function

Last Updated : 05 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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
# 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"
Output :
 method : 0

 method : -1

 method : -1

Example #2 : Ruby
# 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"
Output :
 method : -1

 method : -1

 method : 0


Next Article

Similar Reads