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






