Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Boolean Operators in Ruby
&& Operator
|| Operator
! Operator
Example 1
Example 2 
Frequently Asked Questions
What does Ruby's |= stand for?
In Ruby, what is a lambda?
What does || mean in Ruby?
In Ruby, what is yield?
Last Updated: Mar 27, 2024

Boolean Operators in Ruby.

Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


Ruby is an excellent language for making desktop programs, static webpages, data processing services, and even automation solutions. Web servers, DevOps, and web scraping and crawling are all examples of where it's employed. When you bring in the Rails application framework's features, you can accomplish even more, especially with database-driven web apps.

Ruby language

In this article, we will discuss some Assignment on Boolean Operators in Ruby to enable you to go to higher levels of expertise. Before studying this article, you need to have a basic understanding of computer programming terms and ruby. If this isn't the case, come back here after reading our Ruby article.

Now, let's discuss the types of Boolean operators in ruby.

Boolean Operators in Ruby

boolean operators

Logical expressions, like comparison expressions, return a true (1) or false (0) value when processed. Boolean operators in ruby combine two comparisons and return the true (1) or false (0) value depending on the results of the comparisons. We have the following types of Boolean operators in ruby:

&& Operator

AND operator

If both left and right operands or expressions are true, it will return true. Otherwise, it will return false.


|| Operator

This is the another type of Boolean operator in ruby, which is called OR.

If any of the operands or expressions is true, then it will return 1 and if all of them are false, it will return 0.


Or operator


! Operator

Boolean NOT operator is used to inverse the current decision. Say, if the current state is true, Boolean NOT (!) operator will make it false.

Working of NOT operator

There are two variations of each of these three operators:

and and &&

or and ||

not and !

Now, let's talk about the "operator precedence". It is what distinguishes the operators from one another.

You know from arithmetic that the result of 1 + 2 * 3 is 7, not 9. This is so that the addition operator + can be used after the stronger-binding multiplication (*) operator. In other words, 1 + 2 * 3 is equivalent to 1 + (2 * 3), not 1 + 2 * 3.

The Ruby operators &&, ||, and ! bind stronger than their counterparts and therefore they come before and, or, and not. 

Let us see some examples of Boolean operators in ruby.

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

Example 1

# Variables
a = 10
b = 20
c = 30

# using && operator
if a == 10 && b == 20 && c == 30
puts "Boolean AND Operator"
puts result = a * b * c

# using || operator
puts "Boolean OR operator"
if a == 10 || b == 20
puts result = a + b + c

# using ! operator
puts "Boolean Not Operator"
puts !(true)


Boolean AND Operator
Boolean OR operator
Boolean Not Operator

Example 2 

puts ("logical operators in Ruby")
ruby = "x"
programming = "y" 
      if ruby == "foo" && programming == "bar"
        puts "&&"
      if ruby == "foo" and programming == "bar"
        puts "&& and"
      p, q, r, s = 1, 2 ,3 , 4
      if p == 1 && q == 2 && r == 3 && s == 4
        puts sum = p + q + r + s
      programming = "ruby"
      if ruby == "foo" || programming == "bar"
        puts "||"
      if ruby == "foo" or programming == "bar"
        puts "|| or"
      ruby = "awesome"
      if ruby == "foo" or programming == "bar"
        puts "|| or"
       puts "nothing!"
      if not (ruby == "foo" || programming == "bar")
       puts "sorry!"
      if !(ruby == "foo" or programming == "bar")
       puts "nope!"


logical operators in Ruby


We have discussed &&, || , !, Boolean operators in ruby. Now, let's see some FAQS related to them.

Frequently Asked Questions

What does Ruby's |= stand for?

It's short for or-equals-to. It first checks whether the value on the left is defined, after which it uses it. If it isn't, use the right-hand value.

In Ruby, what is a lambda?

A lambda is a Ruby object that is comparable to a proc. A lambda, unlike a proc, requires a fixed number of arguments to be supplied to it, and instead of returning immediately, it returns to its calling procedure.

What does || mean in Ruby?

A conditional assignment operator is denoted by the symbol ||=. It functions similarly to =, with the exception that it does nothing if a variable has already been assigned.

In Ruby, what is yield?

Yield is a Ruby keyword that allows a developer to give an argument to a block. The amount of arguments that can be passed to the block is unlimited.


In this article, we have discussed Boolean Operators in Ruby. We looked in-depth using examples.

We hope this article on Boolean operators in ruby helps you to learn something new. And if you're interested in learning more, see our posts on 8 reasons why Ruby should be your first language!RubyIntroduction to Ruby on RailsRuby on rails for your next web development project!Ruby on Rails.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Thank you for reading this post :-)

Feel free to upvote and share this article if it has been useful for you.

Happy Learning!

Previous article
Comparison(<, <=, >, >=, and <=>) Operations in Ruby
Next article
Ranges and FlipFlops in Ruby
Live masterclass