false vs nil and the Idea of “Truthiness”
false vs nil
false is a Boolean value which evaluate to false in a condition.
nil represents the nothing. It shall not be confused with emptyness.
"" is an empty string, but it is not nothing, therefore different from
nil evaluates to false in a Boolean context.
nil are two different objects and their values are also different; respectively false and nil. However, in Ruby, the value they evaluate to in a Boolean context is false for both (see Truthiness below for more information).
The reason there are
false and not just
false is they serve different purpose:
trueare used in Boolean expressions
nilis the value returned when something went awry or when there’s nothing to return (not even emptyness)
In Ruby, every value except
nil evaluates to true in a Boolean context. Another way to say it is that every value except the two above are “truthy”. On the other hand, this also means that
nil are “falsey”.
A common mistake made when first learning about truthiness is to think “well, this is just like
As the result of the comparison being done is often the same whether you use a Boolean or truthiness, the distinction between the two may seems overzealous at first, but there are good reasons to distinguish them ().
When describing a piece of code including truthy or falsey, one must make sure to say so and not make the common mistake of calling them just “true” or “false”, or even say they are equal to true or false.
However, one can say that a truthy or a falsey evaluates to either