What is a boolean?
A boolean is a value used in a logic statement to say if something is considered true or false.
This can be used to make decisions.
In Ruby we don’t have a Boolean class, but we have boolean objects!
We have true
& false
.
Which are the singleton objects of TrueClass
& FalseClass
.
You get a boolean value when you use methods like:
empty?
all?
match?
And compare things with an equal sign:
1 == 1 # true
Keep in mind that ==
in Ruby is also a method, this means that the behavior can change depending on how this method is implemented.
Truthy & Falsy Values
What is a truthy value?
It’s a value that’s considered true
in a boolean context, like an if statement.
Everything in Ruby is truthy but these two:
false
nil
These two values, and ONLY these two, we call “falsy”.
Booleans in Practice
This means that if you have a condition…
Like this:
if bacon puts "we got bacon" end
Ruby checks if bacon
is truthy (anything but false
/ nil
) before printing the string.
In other words:
You don’t have to check for nil if you aren’t calling a method on bacon
.
The Safe Navigator
Sometimes you want to call a method on the object.
Like this:
if bacon.stock # ... end
This will give you an error if bacon
is nil, to avoid this you can do the following…
Example:
if bacon&.stock # ... end
This &.
is called the safe navigation operator & it was introduced in Ruby 2.3.
Boolean Methods
Have you seen these methods ending in a question mark?
Like empty?
.
We call these “predicate methods” & by convention they always return either true
or false
.
You can write your own:
def published? # ... end def ready? # ... end
This is a great pattern that will make your code feel more like Ruby.
Boolean Parameters Aren’t a Good Idea
We just covered boolean methods, which are great, but you want to avoid is boolean parameters.
Example:
def bacon(raw) end bacon(false)
When you look at bacon(false)
you have no idea what this false
means.
You would have to dig into the code to find out.
On top of that, a boolean value means that your method is going to be more complex than it needs to be.
The solution?
Split the method in two, or design your code in a way where this isn’t necessary.
Boolean Logic
TrueClass & FalseClass implement a few methods.
Like to_s, and inspect.
But more interesting are:
&
|
^
What are these strange-looking methods?
Boolean logic.
Here’s a boolean logic table:
Name | Symbol | TRUE / TRUE | TRUE / FALSE |
---|---|---|---|
AND | & | true | false |
OR | | | true | true |
XOR | ^ | false | true |
Example:
true & true
While we often don’t use boolean logic in our code it forms the foundation of how computers work, so it’s interesting to know about it.
Summary
You have learned about boolean values in Ruby! Remember that everything is “truthy”, with the only exceptions being false
& nil
.
Don’t forget to share this article so more people can find it 🙂
Thanks for reading.
bacon&.stock > 10
this will still raiseundefined method
>’ for nil:NilClass` if bacon in nil.I feel, the correct way is
bacon&.stock.to_i > 10
correct me if wrong.
Yes. You’re right!
Thanks for pointing this out 🙂