Ruby | Integer <=> function
Last Updated :
19 Mar, 2024
The <=> is an inbuilt method in Ruby returns three values -1, 0 or +1. It returns -1 if the number is less than the given number, 0 if both are same, 1 if it is greater than the given number.
Syntax: num1 <=> num2
Parameters: The function accepts no parameter.
Return Value: It returns -1, 0, +1 as stated above.
Example 1:
num1 = 6
num2 = 3
print num1 <=> num2
|
Output:
1
Example 2:
num1 = 2
num2 = 3
print num1 <=> num2
|
Output:
-1
Example 3:
num1 = 3
num2 = 3
print num1 <=> num2
|
Output:
0
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...