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 ofTrueClass
.false
is an instance ofFalseClass
.
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
isfalse
because only one value istrue
.logged_in || is_admin
istrue
because one of them istrue
.!logged_in
becomesfalse
becauselogged_in
istrue
.
Note: Ruby has 3 logical operators.
&&
means and — returnstrue
only if both sides aretrue
.||
means or — returnstrue
if at least one side istrue
.!
means not — flips the value (makestrue
intofalse
, 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
ornil
.
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
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.