Home »
Ruby »
Ruby Programs
Ruby program to demonstrate the <=> operator
Ruby Example: Write a program to demonstrate the <=> operator.
Submitted by Nidhi, on December 14, 2021
Problem Solution:
In this program, we will create 3 variables and initialize them with some values. Then we will use the "<=>" operator to compare the value of variables.
The "<=>" operator returns an integer value.
- Returns 0 when both variables are equal.
- Returns 1 when the first operand is greater than the second operand.
- Returns -1 when the first operand is less than the second operand.
Program/Source Code:
The source code to demonstrate the "<=>" operator is given below. The given program is compiled and executed successfully.
# Ruby program to demonstrate the
# "<=>" operator
num1 = 10;
num2 = 10;
num3 = 30;
print (num1 <=> num2),"\n";
print (num2 <=> num3),"\n";
print (num3 <=> num1),"\n";
Output:
0
-1
1
Explanation:
In the above program, we created three variables and initialized 10, 10, 30 respectively. Then we used the "<=>" operator to compare values of variables and printed the returned value.
Ruby Basic Programs »