Ruby Logical Operators Tutorial

In this section, we will learn what the Logical 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 and Ruby Comparison operators.

What is Logical Operators in Ruby?

The Logical Operators are those type of operators that allow us to combine the result of more than one expression and get a final result based on the output of those expressions.

For example, let’s say your program is designed to decide whether a person can enter to a bar. There are two conditions in your program to be checked:

– If the age of the person is over 21 AND if the person has enough money!

Here we have two conditions that both should result true otherwise the target user shouldn’t be able to enter to the building.

This is an example where we can use one of the logical operators called `and`. Basically, using the `and` operator we can combine the result of two expressions (for example `age >21 and money >100`) and only if both expressions resulted true then the final result will be true as well.

Alright, the `and` operator is just one example of logical operators. But we have more operators in this category and so let’s go and see these operators now.

List of Logical operators in Ruby

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

Operator Description
and Using this operator, we can combine the result of two expressions and decide whether the final result should be true or false.
or Using this operator, we can check two expressions and, based on the results of those expressions, we can decide if the final value should be true or false.
not Using this operator, we can negate the result of an expression that results either true or false. This means if the result is true, then the value of false returns instead and vice versa.
&& This operator acts the same as the `and` operator and so it will combine the result of two expressions and decide whether the final result should be true or false.
|| This operator acts the same as the `or` operator.
! This operator acts the same as the `not` operator.

Ruby AND `&&` Operator:

The `and` or `&&` operator is used to combine the result of two expressions and if and only if the result of both expressions were true then the final result will be true as well. Otherwise, even if one of the expressions resulted false, then the final result will be false as well.

Notes:

  • There’s no difference between the `and` and `&&` operator. We can use any one of them.
  • The expressions this operator takes could be from a simple value like an integer, float, string etc. which Ruby can try to convert into true or false (based on the value of the expression) or a comparison operation using relational operators that result a boolean value (true or false).
  • This operator short-circuits! This means if the result of the first expression was false, it won’t check the result of the second expression anymore! This is because based on the rules of the operator, even if one expression results false, the entire result will be false! So there’s no point on checking the other expression when the first one already resulted false.

Example: using AND && operator in Ruby

age = 23

money = 150

if age >= 21 && money >= 100

    puts "This person can enter to the bar"

end

Output:

This person can enter to the bar

Here, in the condition of the if statement, we have two expressions for the && operator. If both of these expressions resulted true, then the final result will be true as well and so the body of the if statement will run as a result.

Now we can see that the `age` variable is higher than 21 and so the result of the first expression (the left expression) is true and now the right side expression will be checked which is to see if the value of the `money` variable is higher than 100. So because the second expression is also true, then the final result will be true as well and for this reason the body of the if statement ran.

Note that we could replace the `&&` operator with the `and` operator and the result would be the same.

Example: using the `and` operator in Ruby:

age = 23

money = 150

if age >= 21 and money >= 100

    puts "This person can enter to the bar"

end

Output:

This person can enter to the bar

Ruby OR || Operator:

Just like the `and` operator, the `or` operator is used to check the result of two expressions that result either a truthy or falsy value and, based on those results, return a final boolean value.

But here’s the fundamental difference:

While the AND operator returns the value true only if both involved expressions result true, the OR operator only looks for one expression to result true! Basically, if the first expression resulted false, this operator will check the second expression to see if that results true or not. Now, if the second expression resulted true, then the last result of this operator will be true as well. But if both expressions resulted false, then the last result of this operator will be false as well.

Example: using OR || operator in Ruby

full_name = "John Doe"

language = "Ruby"

if language == "Java" or language == "Ruby"

     puts "Hello #{full_name}, you can apply for a job position in our company"

end

Output:

Hello John Doe, you can apply for a job position in our company

In this example, if the value of the `language` variable was either equal to `Ruby` or `Java` then the result of the `if` statement’s condition will be true and so its body will run as a result.

Note that we could replace the `or` operator in the example above and still get the same result.

Example: using the `||` operator in Ruby

full_name = "John Doe"

language = "Ruby"

if language == "Java" || language == "Ruby"

     puts "Hello #{full_name}, you can apply for a job position in our company"

end

Output:

Hello John Doe, you can apply for a job position in our company

Ruby Not `!` Operator (AKA Reverse the Result Operator):

The not operator is used to reverse the result of a boolean expression. For example, if we use this operator behind an expression that is true, then the result will be false. Or if we use this operator behind a false value, then the final result will be true.

Example: using the not operator in Ruby

if !false

    puts "The body of the if statement ran..."

end

Output:

The body of the if statement ran…

Note that the condition of the if statement was false at first but because we’ve used the not operator behind this result, then the final result reversed and we got the value true as a result.

Also note that we could replace the `!` operator with the `not` operator and still get the same result:

if not false

    puts "The body of the if statement ran..."

end

Output:

The body of the if statement ran…
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies