In this lesson, we’ll explore Ruby booleans and conditional statements. We’ll take a look at nil
and how Ruby views truthiness and falsiness and we’ll also get a chance to explore Ruby operators such as &&
, ||
, and !
.
true
and false
are objects like any other object in Ruby. We can even take a look at their classes:
> true.class()
=> TrueClass
> false.class()
=> FalseClass
We can use booleans to check whether a statement is true or false. Here are a few examples:
> 5.odd?()
=> true
> 6 < 5
=> false
> "cat".eql?("dog")
=> false
> (1..10).cover?(5)
=> true
nil
is Ruby's concept of nothingness. You’ll see nil
regularly in Ruby. Here’s an example:
> letters = ["a", "b", "c"]
> letters[10]
=> nil
There’s no element at index 10, so nil
is returned. Like everything else, nil
is an object; it belongs to NilClass
.
nil
is considered falsy in Ruby; it's treated the same as false
. While false
and nil
are falsy, everything else is truthy. This is important to remember when using branches or booleans; if a statement returns nil
, then it’s equivalent to false
.
Booleans are commonly used for branching. Here's an example. Let's write a method that checks whether someone is old enough to drink:
def old_enough?(age)
if age >= 21
"You can drink."
else
"You can't drink."
end
end
> old_enough?(15)
=> "You can't drink."
> old_enough?(22)
=> "You can drink."
if...else
statements have a simple syntax. The following example uses pseudocode to illustrate this syntax:
if condition_is_met
do_this
else
do_that
end
We could also simply write an if
statement if we don't need an else
condition:
if condition_is_met
do_this
end
If we have additional conditions, we can use the elsif
keyword.
if condition_is_met
do_this
elsif condition_two_is_met
do_that
else
do_another_thing
end
We can add as many elsif
statements as we need. We'll take a look at another example of a method using conditionals with elsif
in a moment.
If we only need to return whether a condition is true or false, we can take advantage of Ruby's implicit return to refactor our code. Here's an example:
def can_vote?
if self > 18
true
else
false
end
end
We can refactor this method to make it much shorter:
def can_vote?
self > 18
end
The method will implicitly return whether the statement is true or false. You may have also noticed that the method ends with a ?
. Ruby doesn't care whether you add question marks to your methods, but you should only use them for methods that return boolean values. Just as we should only create !
methods if the method has a non-bang counterpart (and generally only when the bang method is destructive), ?
methods should return booleans. Ruby won't enforce these conventions, but they are important for readability. In the real world, other developers will often be reading our code and we should always be polite.
Sometimes we need to consider more complex conditions when we're branching. In these cases, we can use the &&
(and) and ||
(or) operators. Here's an example. Let's say we want to make sure a kid is both tall enough and old enough to ride a roller coaster.
def big_enough(height, age)
if (height > 5) && (age > 8)
"You can ride the roller coaster!"
elsif (height > 5)
"You're tall enough but not old enough..."
elsif (age > 8)
"You're old enough but not tall enough..."
else
"You aren't tall enough or old enough to ride yet."
end
end
The &&
method returns true
only if both statements are true
. Note that we put parentheses around each of our statements; this ensures our code won't break and improves readability. If we don't include parentheses, the Ruby interpreter might get confused about the order of operations and throw an error.
Let's try out the ||
(or) operator now. Let's say we want to see if a number is less than 5 or greater than 10. Our method might look like this:
def in_range?(number)
(number < 5) || (number > 10)
end
This method checks if one of the statements is true and then uses the implicit return to return a boolean value. Note that we've added a ?
here to signify the method returns a boolean.
Note: You may also see |
and &
used in Ruby code. These are called Bitwise Operators. While they will often give us the same results as ||
and &&
, there are subtle differences which we won't detail in this lesson.
There's one more operator that's particularly useful with conditionals. !
means not. Here's our can_vote()
method updated to use the not operator:
def can_vote?
!(self < 18)
end
Here we're saying that self
can't be under 18 in order to vote. This example illustrates why the !
operator can sometimes be a little confusing. If anything, we made the can_vote?()
method less clear by using the not operator. While this operator can often be useful, we should always strive to make our code as clear and readable as possible. Use the !
operator only when it adds further clarity to your code.
In this lesson, we've covered the basics of booleans, conditionals and the &&
, ||
and !
operators. We're now ready to move on to looping!
true
and false
are objects like any other object in Ruby:
> true.class()
=> TrueClass
> false.class()
=> FalseClass
Methods that return a boolean should end with a ?
.
nil
is Ruby's concept of nothingness. nil
is considered falsy in Ruby; it's treated the same as false
. While false
and nil
are falsy, everything else is truthy. This is important to remember when using branches or booleans; if a statement returns nil
, then it’s equivalent to false
.
if...else
statements have a simple syntax. The following example uses pseudocode to illustrate this syntax and includes the elsif
keyword.
if condition_is_met
do_this
elsif condition_two_is_met
do_that
else
do_another_thing
end
The &
(and) operator returns true
only if the statement to the left and the right of &
are both true
.
The |
(or) operator checks if either the statement to the left or the right of |
is true
. If either or true
, the entire statement evaluates to true.
The !
(not) operator returns the opposite value of a statement.
Lesson 5 of 10
Last updated August 7, 2022