×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to demonstrate the <=> operator

Last Updated : December 15, 2025

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!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.