Ruby Boolean

A boolean is a value that can either be true or false.

Here's a simple example of a boolean in Ruby. You can read the rest of the tutorial to learn more.

Example

is_open = true
is_active = false

puts is_open   
puts is_active


# Output:
# true
# false

Here, the variable is_open holds the boolean value true, and is_active holds false.


Boolean Values

In Ruby, the two boolean values are true and false. These values are not just keywords — they are actual objects in Ruby.

# Use the .class method to find the class of true and false
puts true.class     
puts false.class

Output

TrueClass
FalseClass

This means,

  • true is an instance of TrueClass.
  • false is an instance of FalseClass.

Note: TrueClass and FalseClass are built-in classes in Ruby.


Boolean Expression

A boolean expression is an expression that is evaluated as either true or false.

In Ruby, these expressions often involve comparisons between values, such as checking if numbers are equal, or if they're greater than or less than others. For example,

puts 10 > 5         # true
puts 3 == 3         # true
puts "cat" == "dog" # false
puts 7 <= 2         # false

Each of these expressions returns a boolean value: either true or false.


Boolean with Logical Operators

In Ruby, you can use logical operators to combine or change boolean values. The resulting expression will give a new boolean result.

Let's take a look at an example.

logged_in = true
is_admin = false

puts logged_in && is_admin   # false
puts logged_in || is_admin   # true
puts !logged_in              # false

In this example:

  • logged_in && is_admin is false because only one value is true.
  • logged_in || is_admin is true because one of them is true.
  • !logged_in becomes false because logged_in is true.

Note: Ruby has 3 logical operators.

  • && means and — returns true only if both sides are true.
  • || means or — returns true if at least one side is true.
  • ! means not — flips the value (makes true into false, and vice versa).

Using Booleans in if...else

Booleans are often used in an if...else statement to decide which code should run. For example,

is_raining = true

if is_raining
  puts "Take an umbrella!"
else
  puts "Enjoy the sunshine!"
end

Output

Take an umbrella!

Here, is_raining is a boolean. Since it's true, the if block runs. If it were false, the else block would have run.


Truthy and Falsy Values

In Ruby, it's not just true and false that behave like booleans. Ruby also treats other values as true or false when used in conditions.

  • A truthy value is anything Ruby considers true in a condition.
  • A falsy value is something Ruby considers false in a condition.

Falsy Values

There are only two falsy values in Ruby:

  • false
  • nil

Truthy Values

Every other value is considered truthy, including:

  • 0 (zero)
  • "" (empty string)
  • [] (empty array)
  • Any object that is not false or nil.

Even if they seem "empty" or "zero," Ruby still sees them as true. Let's see how this works with an example.

puts !!false    # false
puts !!nil      # false

puts !!0        # true
puts !!""       # true
puts !![]       # true

Note: !! is used to convert any value to its boolean form.


Frequently Asked Question

What is a predicate method?

In Ruby, some methods return boolean values. They are called predicate methods and usually end with a question mark (?). For example,

puts "".empty?       # true 
puts [1].empty?      # false
puts 4.even?         # true

Here, empty? and even? are predicate methods:

  • empty? checks whether a string or array is empty.
  • even? checks whether a number is even.
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