Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Boolean?
3.
Boolean in Ruby 
4.
Boolean Operators
5.
Creating a Boolean Method
6.
True, False, and Nil Keywords
6.1.
TrueClass
6.2.
FalseClass
6.3.
NilClass
7.
Why doesn't Ruby have a Boolean Class?
7.1.
Dynamic Typing
8.
Frequently Asked Questions
8.1.
Is there a boolean operator in Ruby?
8.2.
What is the difference between the OR ‘&&’ and AND operators ‘ ||’ in ruby?
8.3.
What are a truthy value and falsy value?
8.4.
In Ruby, what does ‘!= nil’ mean?
9.
Conclusion
Last Updated: Mar 27, 2024

Boolean in Ruby

Author Kanak Rana
2 upvotes

Introduction

Ruby is the interpreted high-level programming language that supports various programming paradigms. It was designed with programming productivity and ease of use in mind. 

In Ruby, everything is an object and includes primitive data types. Yukihiro "Matz" Matsumoto created it in Japan in the mid-1990s. Ruby uses garbage collection and just-in-time compilation, and it is dynamically typed. Ruby's development aimed to create a user interface between human programmers and the underlying computational machinery.

What is a Boolean?

The use of Boolean data types is straightforward. A Boolean data type has only two possible values: 'true’ or ‘false'. True will be stored as 1, and false will be stored as a 0. 

A boolean data type can represent ON/OFF and YES/NO.

Boolean in Ruby 

A boolean value is used in a logic statement to indicate whether something is ‘true’ or ‘false’. This information can be used to make choices. There is no such thing as a Boolean class in Ruby, but there are boolean objects! 

There are two types of truths: 

  • True 
  • False
     

TrueClass and FalseClass both have singleton objects.

 

In the conversions of Boolean types in the context of type conversion, the Boolean values deserve special attention. 

  • True and false have two methods that return "true" and "false", respectively, but no other conversion methods are defined in Ruby. There are also a number of methods for converting non-Boolean values to Booleans. False is the same as 0 in some languages or can be converted to  1  from 0. 'True' and 'false' are distinct objects in Ruby, and there are no implicit conversions that convert other values to 'true' or 'false'.
     
  •  ‘Boolean operators in Ruby’ and ‘conditional and looping constructs based on Boolean expressions’ can work with values other than ‘true’ and ‘false’.
     
coding_ninjas  =true   # Similarly, a false condition is also implemented.
if(coding_ninjas)
puts "We are coding_ninjas."
else
puts "No, we are not coding_ninjas."


Output

We are coding_ninjas.

 

Boolean Operators

In Ruby, there are three main boolean operators:

NOT(!) For an && ("and") to evaluate true, both values of either side of the symbol must be true.

For example-

false || true #=> true

 

AND(&&)

For an || ("or") to evaluate true, only one value on either side of the symbol must be true.

 

For example-

true && true #=> true

true && false #=> false

 

OR(||) For a ! ("not") reverses the logical state of its operand: if a condition is true, then ‘!’ will make it false; if it is false, then ‘!’ will make it true.

For example-

!true #=> false

!false #=> true

 

 

Creating a Boolean Method

Boolean Methods, also known as Predicates or Query. Methods that end in a question mark (?) know as Query.

It is assumed that by using these methods, and we can ask a specific question and obtain specific information about the object. Ruby, for example, has the method 'any?' It allows us to ask an array if it contains any elements and receive a 'true' or 'false' response.

puts [].any?                                 # Is there an array contains elements? No so false
puts [ "cat", "dog", "tiger"].any?           # Is there an array contains elements? Yes so true


Output

false 

true


To sum up, users can also create the boolean method and must return true or false.

True, False, and Nil Keywords

True, false, and nil are Ruby keywords. They represent truth and falsehood, yes or no, respectively. The value nil is reserved for indicating the absence of value. Each of these keywords yields a unique object. 

True returns a singleton instance of TrueClass as an object. It's worth noting that Ruby lacks a Boolean class. The object is the superclass of both TrueClass and FalseClass. 

If you want to evaluate whether a value is nil, you can compare it to nil or use the method.

nil?: a == nil       # Is a nil?
a.nil?               # Another method to check


True, false, and nil are all objects, not numbers. False and nil are not the same as zero, and true is not the same as one. When Ruby needs a Boolean value, nil behaves like false, and any other value behaves like true.

# Ruby program to use true and false 

p = 2
q = 4

if p == q
puts true

   # If Condition is true, "True! p and q are equal" is displayed.
     
Else
puts false
  
    # If Condition is false, "False! p and q are not equal" is displayed
    
end


Output

false

TrueClass

True represents a logically true value in boolean expressions and is the only class instance. TrueClass. True can be used in logical expressions with the help of the class's operators.

FalseClass

The only instance of class FalseClass is the global value false, which represents a logically false value in boolean expressions. The class contains operators that allow false to participate in logical expressions correctly.

NilClass

The singleton object's class is nil. Ruby includes a built-in class called NilClass. This class can't be instantiated. When a message is sent to nil, it is received by a hard-coded C-level "class" called rb.NilClass, is equivalent to Ruby's NilClass. The news will be found and sent among the new receiver's instance methods.

Why doesn't Ruby have a Boolean Class?

While Ruby has true and false types, they don't share a standard Boolean class, a stumbling block for many programmers migrating from other languages. What is the reason for this?

A unique data type called a Boolean represents true or false values in some programming languages. Others make do by representing true and false values from other data types. For example, the integers 0 and 1 stand for "false" and "true", respectively.

Dynamic Typing

To understand why Ruby lacks a Boolean class, we must first consider Ruby's dynamically typed nature. We don't need to declare the type of an argument or a variable before assigning a variable to it in Ruby, unlike in statically-typed languages like Java, C++, or Haskell. So, in this method, the name is the only thing that indicates that the single argument is expected to be a true or false value. It is not necessary to declare a type.

Frequently Asked Questions

Is there a boolean operator in Ruby?

Ruby is an outlier in that it needs a Boolean data type, despite having explicit values representing true and false. Instead, in Ruby, the sole instance of TrueClass represents truth, and the isolated example of FalseClass illustrates falsehood.

What is the difference between the OR ‘&&’ and AND operators ‘ ||’ in ruby?

Ruby is first in the precedence list. The operator && takes precedence over the operator and. However, this is not a problem unless used with operators that fall between the two, the ternary and assignment operators. you can take a look at the above table.

What are a truthy value and falsy value?

In a boolean context, such as an if statement like the above example, it's a value considered True and false, respectively.

In Ruby, what does ‘!= nil’ mean?

Nil is a unique value in Ruby that represents the absence of any value. Nil is a ‘NilClass’ object. Nothing or void is referred to as nil in Ruby.

Conclusion

In this blog, we've learned about boolean values in Ruby. Keep in mind that everything is "truthy". To sum up, we have discovered that: Boolean methods end in a question mark (?) and must return true or false. You can construct your Boolean methods in Ruby.

If you are interested in ruby, you can check out  Ruby and Ralis, and check out more articles here:

Refer to Ruby Documentation here. 
 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, and System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Learning Ninja! 🥷

Live masterclass