As a coding newbie, I still question and sometimes overthink concepts that might seem pretty basic to most programmers. During my pre-bootcamp preparation journey, one of the topics I had to focus on was Ruby Truthiness and Falseness. It triggered my curiosity.
After doing some research, asking questions to my Flatiron School instructors, and discussing with my boyfriend, I discovered the contrast between Ruby's truthiness versus other languages.
Let's go over some questions and present examples to help explain how it works.
Why should booleans never confuse you in Ruby?
If you look at the cover image of this article, you should be able to identify how many objects there were on the different walls. There was nothing or in Ruby terms, nil. nil
represents nothingness. It is also considered "falsy". Yuuup! It's because it is empty. So, is there any other natively falsy object? NO, except for false
itself, everything else in Ruby is "truthy". In contrast to other languages like JavaScript, where empty strings ""
and 0
are falsy. Ruby considers them truthy because they still represent something and not nothing.
Truthiness and Falseness are language dependent based on what the implementers chose.
def check_truthy(var_name, var)
is_truthy = var ? "truthy" : "falsy"
puts "#{var_name} is #{is_truthy}"
end
check_truthy("false", false)
check_truthy("nil", nil)
check_truthy("0", 0)
check_truthy("empty string", "")
check_truthy("\\n", "\n")
check_truthy("empty array", [])
check_truthy("empty hash", {})
=> false is falsy
nil is falsy
0 is truthy
empty string is truthy
\n is truthy
empty array is truthy
empty hash is truthy
When do we receive nil?
nil
is a special object in Ruby. When we call a method, we know we are going to get something in return. right? Sometimes, when there is nothing to return, it will return nil
, which represents nothing.
Just like our instructor "Kim" would say: "If it sounds like I am repeating my self. It is because I am".
We could go on and on the philosophical implications of nil, but instead, we’ll just look at how it is used in practice:
Let's say we have this magazines hash;
magazines = { :fashion => "style", :sport => "gogo", :travel => "vacation" }
p magazines[:sport]
This will print out "gogo"
. However, what happens if we try to receive the value for a key that has not been defined on the hash?
p magazines[:health]
This will return;
'nil'
What kind of methods return a boolean?
You might have seen some methods that ends with a question mark.
Example: .even?
2.even?
=> true
3.even?
=> false
We call these "Boolean Methods" or "Predicate Methods", because, they return a boolean value. Conventionally, the methods that end with a question mark are reserved for boolean methods.
Apart from the Ruby built-in boolean methods, we can also create our own.
Watch:
def multiples_of_7?(x)
(x % 7) == 0
end
multiples_of_7?(21)
=> true
If we try the same method, passing 5 as an argument, it would then output false
. Why? Simply because 5 isn't a multiple of 7.
Overall, booleans are simple. Although, it could get tricky in some occasions. Make sure you are able to differentiate between TrueClass class (class that groups true boolean objects) and a FalseClass class (class that groups false boolean objects). Be aware of emptiness and ruby specifics.
Happy coding beginnings!
Top comments (0)