Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Different Comparison Operations
2.1.
Less than(<) Operation
2.1.1.
Example
2.1.2.
Output
2.2.
Less than or Equal to(<=) Operation
2.2.1.
Example
2.2.2.
Output
2.3.
Greater than(>) Operation
2.3.1.
Example
2.3.2.
Output
2.4.
Greater than or Equal to(>=) Operation
2.4.1.
Example
2.4.2.
Output
2.5.
Combined Comparison(<=>) Operation
2.5.1.
Example
2.5.2.
Output
3.
Frequently Asked Questions
3.1.
What are the Assignment Operators in Ruby?
3.2.
What are the Arithmetic Operators in Ruby?
3.3.
What are the Logical Operators in Ruby?
4.
Conclusion
Last Updated: Mar 27, 2024

Comparison(<, <=, >, >=, and <=>) Operations in Ruby

Author Aniket Majhi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Hello and Welcome, readers! We hope you are doing well.

Ruby is an open-source, high-level, general-purpose programming language designed by Yukihiro Matsumoto in the mid-1990s. The focus of developing this programming language was on simplicity and programming productivity

If you want to learn about Ruby, check out our articles on Ruby.

Today, In this blog, we will discuss the Comparison(<, <=, >, >= and <=>) Operations on Ruby with proper explanation and implementation. From this article, you will get a clear idea of the comparison operations, so follow the article till the end.

So, without further ado, let’s start our discussion.

Different Comparison Operations

In Ruby, to compare different things, we have the comparison operators. Here in this section, we will focus on five different comparison operators in Ruby, i.e. Less than(<)Less than or equal to(<=)Greater than(>), Greater than or equal to(>=) and Combined comparison operator(<=>).

Less than(<) Operation

The Less than(<) operation in Ruby returns true if the lefthand side value of the operator is lower than the righthand side value. Otherwise, it returns false.

For example, consider the below operations:

A < B, for this operation, it returns true if A has a lower value than B.

Like, 10 < 15 (returns true), 20 < 10(returns false).

Example

a = 10
b = 15
 
puts("Comparison Result(a < b): ")
puts(a < b)
puts("Comparison Result(b < a): ")
puts(b < a)

Output

Comparison Result(a < b): 
true
Comparison Result(b < a): 
false

Less than or Equal to(<=) Operation

The Less than or Equal to(<=) operation in Ruby returns true if the value on the operator's left-hand side is less than or equal to the value on the operator's righthand side.

For example, consider the below operations:

A <= B, for this operation, returns true if A has a value lower or equal to the B.

Like, 10 <= 15 (returns true), 15 <= 15(returns true), 20 <= 10(returns false).

Example

a = 10
b = 15
c = 10
 
puts("Comparison Result(a <= b):")
puts(a <= b)
puts("Comparison Result(b <= a):")
puts(b <= a)
puts("Comparison Result(c <= a):")
puts(c <= a)

Output

Comparison Result(a <= b): 
true
Comparison Result(b <= a): 
false
Comparison Result(c <= a): 
true

Greater than(>) Operation

The Greater than(>) operation in Ruby returns true if the lefthand side value is higher than the value on the righthand side. Otherwise, it returns false.

For example, consider the below operations:

A > B returns true for this operation if A has a higher value than B.

Like, 10 > 15 (returns false), 20 > 10(returns true).

Example

a = 10
b = 15
 
puts("Comparison Result(a > b): ")
puts(a > b)
puts("Comparison Result(b > a): ")
puts(b > a)

Output

Comparison Result(a > b): 
false
Comparison Result(b > a): 
true

Greater than or Equal to(>=) Operation

The Greater than or Equal to(>=) operation in Ruby returns true if the value on the operator's left-hand side is greater than or equal to the value on the righthand side. Otherwise, it returns false.

For example, consider the below operations:

A >= B, for this operation, it returns true if A has a value higher or equal to the B.

Like, 10 >= 15 (returns false),  15 >= 15(returns true), 20 >= 10(returns true).

Example

a = 10
b = 15
c = 10
 
puts("Comparison Result(a >= b): ")
puts(a >= b)
puts("Comparison Result(b >= c): ")
puts(b >= c)
puts("Comparison Result(c >= a): ")
puts(c >= a)

Output

Comparison Result(a >= b): 
false
Comparison Result(b >= c): 
true
Comparison Result(c >= a): 
true

Combined Comparison(<=>) Operation

The Combined comparison operator(<=>) is a general-purpose comparison operator. The return value of this operator indicates the relative order of the two of its operands. Its return values are the followings:

  • If the value of the lefthand operand is less than the value of the righthand operand, it returns -1.
    For example, 10 <=> 15 returns -1.
  • If the value of the lefthand operand is greater than the value of the righthand operand, it returns +1.
    For example, 15 <=> 10 returns 1.
  • If the value of both the lefthand and righthand operands are equal, it returns 0.
    For example, 10 <=> 10 returns 0.

Example

a = 10
b = 15
c = 10
 
puts("Comparison Result(a <=> b): ")
puts(a <=> b)
puts("Comparison Result(b <=> c): ")
puts(b <=> c)
puts("Comparison Result(c <=> a): ")
puts(c <=> a)

Output

Comparison Result(a <=> b): 
-1
Comparison Result(b <=> c): 
1
Comparison Result(c <=> a): 
0
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What are the Assignment Operators in Ruby?

The Assignment Operators in Ruby are the followings:  

  • Equal Operator(=) 
  • Add AND(+=)
  • Subtract AND(-=)
  • Multiply AND(*=)
  • Divide AND(/=)
  • Modulus AND(%=)
  • Exponent AND(**=)
     

What are the Arithmetic Operators in Ruby?

The Arithmetic Operators in Ruby are the followings:

  • Addition(+)
  • Subtraction(-)
  • Multiplication(*)
  • Division(/)
  • Modulus(%)
  • Exponent(**)
     

What are the Logical Operators in Ruby?

The Logical Operators in Ruby are the followings:

  • Logical AND(&& or and)
  • Logical OR(|| or or)
  • Logical NOT(! or not)
     

Conclusion

In this article, we have extensively discussed the  Comparison(<, <=, >, >= and <=>) Operations on Ruby.

We started with the basic introduction. Then we discussed the followings:

  • Less than Operation
  • Less than or Equal to Operation
  • Greater than Operation
  • Greater than or equal to Operation
  • Combined Comparison Operation
     

Check out this problem - XOR Queries On Tree

We hope that through this blog, you got some ideas regarding Comparison(<, <=, >, >= and <=>) Operations on Ruby. If you would like to learn more, follow our articles on Bitwise NOT(~), AND(&), OR(|) and XOR(^) Operations in Ruby8 Reasons Why Ruby Should be Your First Language, Ruby and Ruby On Rails: How do they differ?History of Ruby and Nonoperators in RubyExplore our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations and much more.!

Do upvote this blog to help other ninjas grow.

Happy Reading!

Live masterclass