ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.