Ruby Comparison And Logical Operators

Ruby Comparison Operators

Comparison operators compare two values and return a boolean result (true or false). For example,

a = 3
b = 2

puts a > b

# Output: true

Here, we used the > comparison operator to check whether a (which is 3) is greater than b (which is 2).

Since 3 is greater than 2, the output is true.

Note: In the above example, a > b is called a boolean expression because it evaluates to a boolean value (true or false).


Commonly Used Comparison Operators

Operator Meaning Example
== Equal to 3 == 5 (Output: false)
!= Not equal to 3 != 4 (Output: true)
> Greater than 4 > 4 (Output: false)
< Less than 3 < 3 (Output: false)
>= Greater than or equal to 4 >= 4 (Output: true)
<= Less than or equal to 3 <= 3 (Output: true)
<=> Combined comparison (returns -1, 0, or 1) 5 <=> 10 (Output: -1)

1. Ruby Equal To Operator

The equal to operator == evaluates to

  • true - If the values of the operands are equal.
  • false - If the values of the operands are not equal.

Note: In Ruby, == is a comparison operator, whereas = is an assignment operator. If you mistakenly use = instead of ==, you might get unexpected results.

Example

num1 = 10
num2 = 15
num3 = 10
string_num = "10"

# Compare numbers with numbers
puts num1 == num2   # false
puts num1 == num3   # true

# Compare number with string
puts num1 == string_num   # false

Here's how this program works:

Expression Result Reason
num1 == num2 false num1 and num2 have different values.
num1 == num3 true num1 and num3 have the same value.
num1 == string_num false num1 and string_num belong to different data types, even if the values look similar.

Comparison Between Different Types

Comparing data of different types in Ruby.

In the previous example, using the == operator to compare the number 10 with the string "10" resulted in a false value, even though both values look similar.

However, there are cases where comparing similar data of different types will give us true. For example,

# Comparing identical integer and float values
puts 4 == 4.0

# Output: true

Although 4 is an Integer and 4.0 is a Float, Ruby considers them equal because their numeric values are the same.


2. Not Equal To Operator

The not equal to operator != evaluates to

  • true - If the values of the operands aren't equal.
  • false - If the values of the operands are equal.

Example

num1 = 10
num2 = 15
num3 = 10
string_num = "10"

# Compare numbers with numbers
puts num1 != num2   # true
puts num1 != num3   # false

# Compare number with string
puts num1 != string_num  # true

Note: The != operator returns the opposite result of the == operator.


3. Greater Than Operator

The greater than operator > returns

  • true - If the left operand is greater than the right operand.
  • false - If the left operand is not greater than the right operand.

Example

num1 = 10
num2 = 15

# Left operand is smaller
puts num1 > num2   # false

# Left operand is greater
puts num2 > num1   # true

# Both operands are equal
puts num1 > 10   # false

4. Greater Than Or Equal To Operator

The greater than or equal to operator >= returns

  • true - If the left operand is greater than or equal to the right operand.
  • false - If the left operand is less than the right operand.

Example

num1 = 10
num2 = 15

# Left operand is smaller
puts num1 >= num2   # false

# Left operand is greater
puts num2 >= num1   # true

# Both operands are equal
puts num1 >= 10   # true

5. Less Than Operator

The less than operator < returns

  • true - If the left operand is less than the right operand.
  • false - If the left operand is not less than the right operand.

Example

num1 = 10
num2 = 15

# Left operand is smaller
puts num1 < num2   # true

# Left operand is greater
puts num2 < num1   # false

# Both operands are equal
puts num1 < 10   # false

6. Less Than Or Equal To Operator

The less than or equal to operator <= returns

  • true - If the left operand is less than or equal to the right operand.
  • false - If the left operand is greater than the right operand.

Example

num1 = 10
num2 = 15

# Left operand is smaller
puts num1 <= num2   # true

# Left operand is greater
puts num2 <= num1   # false

# Both operands are equal
puts num1 <= 10   # true

7. Combined Comparison Operator

The combined comparison operator <=> returns

  • -1 if the left operand is less than the right operand.
  • 1 if the left operand is greater than the right operand.
  • 0 if both operands are equal.

Note: The <=> operator is also known as the spaceship operator.

Example

num1 = 10
num2 = 15

# Left operand is smaller
puts num1 <=> num2   # -1

# Left operand is greater
puts num2 <=> num1   # 1

# Both operands are equal
puts num1 <=> 10   # 0

Ruby Logical Operators

Logical operators return a boolean value by evaluating boolean expressions. For example,

x = 5
y = 3

puts (x < 6) && (y < 5)

# Output: true

Here, && is the logical operator AND.

Since both the boolean expressions x < 6 and y < 5 are true, evaluating them with the && operator also results in true.

Commonly Used Logical Operators

Operator Syntax Description
&& expression1 && expression2 true only if both expression1 and expression2 are true
|| expression1 || expression2 true if either expression1 or expression2 is true
! !expression false if expression is true, and true if the expression is false

1. Logical AND Operator (&&)

The logical AND operator && returns true if both the expressions are true. For example,

x = 2

# Both expressions are true
puts (x < 4) && (3 >= x)    # true

# Only one expression is true
puts (x <= 4) && (x == 3)   # false

# Both expressions are false
puts (x > 4) && (x == 3)   # false

Here's how the program works:

Expression Result Reason
(x < 4) && (3 >= x) true Both expressions are true.
(x <= 4) && (x == 3) false The expression x == 3 is false.
(x > 4) && (x == 3) false Both expressions are false.

2. Logical OR Operator ( || )

The logical OR operator || returns true if at least one expression is true. For example,

x = 2

# Both expressions are true
puts (x < 4) || (3 >= x)    # true

# Only one expression is true
puts (x <= 4) || (x == 3)   # true

# Both expressions are false
puts (x > 4) || (x == 3)   # false

Here's how the program works:

Expression Result Reason
(x < 4) || (3 >= x) true Both expressions are true
(x <= 4) || (x == 3) true The expression x <= 4 is true
(x > 4) || (x == 3) false Both expressions are false

3. Logical NOT Operator ( ! )

The logical NOT operator ! returns true if the specified expression is false and vice versa. For example,

x = 2

# NOT on true
puts !true   # false

# NOT on false
puts !false   # true

# Comparison examples
puts !(x > 3)   # true
puts !(x == 2)  # false

Here's how the program works:

Expression Result Reason
!true false ! inverts the value of true to false
!false true ! inverts the value of false to true
!(x > 3) true ! inverts the false value of (x > 3) to true
!(x == 2) false ! inverts the true value of (x == 2) to false

Frequently Asked Question

What is the difference between Ruby comparison and logical operators?

In Ruby, we use comparison operators to compare two values and find the resulting boolean value (true or false). For example,

# Less than operator
puts 4 < 5

# Output: true

In the above example, we used the < operator to find the boolean value for the condition 4 < 5.

On the other hand, we use logical operators to perform logical operations on boolean expressions. For example,

# Logical NOT
puts !(4 < 5)

# Output: false

Here, the expression 4 < 5 gives us the boolean value true. The ! operator then acts on this boolean value and inverts it to false.

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges