Q:

Ruby program to demonstrate the <=> operator

belongs to collection: Ruby Basic Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to demonstrate the phase() function... >>
<< Ruby program to get the numerator from a rational ...