Ruby Relational (AKA Comparison) Operators Tutorial

In this section, we will learn what the Relational Operators are and how to use them in Ruby.

Note: in this section we’re assuming you’re familiar with the Ruby operators in general.

What are the Relational Operators in Ruby? (Comparison Operators)

The Ruby relational operators (AKA comparison operators) as the name suggests, they are used to compare two values to each other! Basically, these operators are used to see if two values are equal in value, or if one of them is greater than the other!

List of Relational Operators in Ruby

In the table below, you can see the list of comparison operators supported in Ruby.

Operator Symbol Description
Equal == Using this operator, we can check and see if two values are equal or not.
Negate Equal != Using this operator, we can check and see if two values are NOT equal.
Greater than > Using this operator, we can check to see if the left operand is greater than the right operand.
Less than < Using this operator, we can check to see if the right operand is greater than the left operand.
Greater Than or equal to >= Using this operator, we can see if the left operand is greater or equal compared to the right operand.
Less than or equal to <= Using this operator, we can see if the right operand is greater or equal compared to the left operand.
The Spaceship Operator <=> Using this operator, we can declare whether a value is less than, greater than, or equal to another value

Conditional Statements

Before we get into the details of relational operators, you should know that they are mainly used with conditional statements like Ruby `if` statement. This is because comparison operators return a boolean value (the value true or false) and conditional statements like `if` statement work based on these values. Now, in order to give you an example on how comparison operators are used in a Ruby program, we first need to get familiar with conditional statements. For this reason, in this part we will give you an intro to the if statement and see how it works and then use this statement within comparison operators’ examples.

Conditional Statements: If statement

The if statement is used to create a conditional block of codes. Basically, using if statement we can set a condition on top of a block of code and only if that condition resulted true, then the codes within that block will be executed! Otherwise, if the result of the condition in the if statement was false, then the instructions within the if block will be skipped.

Now if you think about it, the use of comparison operators in a conditional statement like the if statement makes a perfect sense! This is because comparison operators return either value true or false and so the result of these operators decides whether the block of an if statement could be run.

Ruby if Statement Syntax:

if condition

# the body of the if statement where we can put instructions…

end

– `if`: in order to create an if statement, we start with the keyword if.

– `condition`: this is where we put an expression that results a truthy or falsy value! For example, we can use comparison operators here because their result is of type boolean (true or false).

Note that other than comparison operators, we are free to use other values as well. Basically, in Ruby only the boolean value `false` and the value `nil` represent a falsy value (which makes a conditional statement like the if statement to be skipped). Other than these values, the rest of values are considered as true in Ruby! For example, a whole number (like 10, -1, 0 etc.) Or a string value etc. are considered as a true value! You’ll learn more about truthy and falsy values in later sections.

– `end`: after setting the condition of an if statement, the new lines below the statement are considered as part of the if block until the `end` statement, which defines the end of the block.

So within the body of the if statement we put the needed instructions and if the condition of the statement was true, then these instructions will run.

If statement Example:

age = 20

if age > 18

    print "The age is above 18"

end

Output:

The age is above 18

Note that here we used one of the comparison operators, which is known as Greater Than operator in the condition of the if statement. Here the condition of the if statement is to see if the value of the age variable is greater than the value 18. If it was (which it is) then the result of the condition becomes true and hence the instructions within the body of the if statement will run.

That’s how we got the message you see on the output.

Alright, now that we have a basic knowledge of the if statement, let’s dig into the relational operators and see how they work.

Ruby Equal `==` Operator:

The Equal operator is used to see if two values are equal or not. If they were, then the return value of this operator becomes true otherwise, the value false will return instead.

Note: when we say equal, that means if both involved operands are pointing to the same memory location. Or basically if both operands are pointing to the same object.

Note: you’ll learn more about objects in later sections.

Example: using equal operator in Ruby

puts "Please enter your name"

name = gets.chomp

if name == "Omid"

    puts "The name is Omid"

end

Output:

Omid

In this example, if users enter the value Omid in the terminal, then the result of this equal operation that is set in the if condition becomes true and so its body will run as a result.

Ruby Not Equal `!=` Operator:

The Not Equal operator `!=` is the opposite of the equal operator and is used to see if two values are not equal.

Note: just like the equal operator, the != operator mainly checks to see if the two operands are pointing to the same memory location!

Example: using not equal operator in Ruby

puts "Please enter your name"

name = gets.chomp

if name != "Omid"

    puts "The name is not Omid"

end

Output:

The name is not Omid

Run this example and put any value other than Omid. You’ll see that because the value is not equal to “Omid”, the result of the condition in the if statement becomes true and so the body of this statement will run as a result.

Ruby Greater Than `>` Operator:

The Greater Than operator is used to seeing if the value on the left side of this operator is greater than the value on the right side. If it was, the return value of this operator will be true, otherwise the value false will return instead.

Example: using greater than operator in Ruby

puts "Please enter your a number"

number = gets.to_i

if number >100

    puts "The value is higher than 100"

end

Output:

The value is higher than 100

In this example, if you enter a value higher than 100, then the condition of the if statement becomes true and so its body will run as a result.

Note: please check the gets method section if you’re not familiar with this method.

Ruby Less Than `<` Operator:

The Less Than operator is used to seeing if the value on the left side of this operator is less than the value on the right side. If it was, then the return value of this operator becomes true, otherwise the value false will return instead.

Example: using less than operator in Ruby

puts "Please enter your a number"

number = gets.to_i

if number <100

    puts "The value is less than 100"

end

Output:

Please enter your a number

10

The value is less than 100

Ruby Greater Than or Equal To `>=` Operator:

The Greater Than or Equal operator is the combination of both > and == operator. This means the two operands are checked for two conditions:

  • If the left side operand is greater than the right side operand, then the final value will be true.
  • If both operands are equal, then the final value of this operator will be true as well.

Example: using greater than or equal operator in Ruby

puts "Please enter a number"

number = gets.to_i

if number >= 100

    puts "The value is either greater or equal to the number 100"

end

Output:

Please enter a number

200

The value is either greater or equal to the number 100

Ruby Less Than or Equal To `<=` Operator:

The Less Than or Equal operator is the combination of both < and == operator. This means the two operands are checked for two conditions:

  • If the left side operand is less than the right side operand, then the final value will be true.
  • If both operands are equal, then the final value of this operator will be true as well.

Example: using less than or equal operator in Ruby

puts "Please enter a number"

number = gets.to_i

if number <= 100

    puts "The value is either less or equal to the number 100"

end

Output:

Please enter a number

100

The value is either less or equal to the number 100

Ruby Spaceship `<=>` Operator:

The spaceship operator is used to check two values (operands) to see which one is greater than the other.

Unlike other operators that return a true or false, this one will return an integer value instead.

The value of this operator will be:

  • 0: if both operands are equal in value.
  • -1: if the left side value is less than the right-hand value.
  • +1: if the left side value is greater than the right side value.

Example: using spaceship `<=>` operator in Ruby

res1 = 10 <=> 20

res2 = 20 <=> 10

res3 = 20 <=> 20

puts res1, res2, res3

Output:

-1

1

0
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies