Truthiness In Ruby Readme

Overview

In this lesson, we'll explain the significance of truthiness in programming and identify boolean values in Ruby.

Objectives

  1. Define control flow and how Boolean values help in implementing it in programming.
  2. List what is truthy and falsey in Ruby.
  3. Use the double bang operator to determine truthiness in Ruby.

Introduction

Truthiness

Many programming languages, including Ruby, have native boolean (true or false) data types. In Ruby they're expressed directly as true and false.

Advanced: This is not the case in all languages. In Python, boolean values are capitalized, True and False, while in Objective-C they are different words YES and NO. However, they all represent the same concept of Boolean logic.

These boolean values come in handy in programming when we want to implement control flow. Control flow is the idea that we can tell our program to execute certain lines of code based upon certain conditions.

Booleans and Flow Control

For example, if I am tired, then I will take a nap. Otherwise, I will keep reading this insightful and informative readme. You could also invert the perspective like in this example:

If it is true that I am tired, then I will take a nap. If it is false that I am tired, then I won't take a nap.

Flow control is predicated on these true-or-false boolean values. The adjectives "truthy" and "falsey" are a programming convention for describing the state of being true and the state of being false.

What this example amounts to is this: we want to be able to use non-boolean values (like strings or integers) in a boolean context; we want to be able to say, "if a certain statement evaluates to true (or is "truthy"), then execute these certain lines of code."

Consequently, Ruby must have a way of determining what counts as true at a given moment—or what is "truthy" versus what is "falsey".

Remember, don't worry about understanding control flow and implementing it right now. This is just to provide some background about why we care about the concept of truthiness in Ruby.

What Is 'truthy' and 'falsey' in Ruby?

Programming languages are software, too! That means the people who built Ruby had to decide what is truthy and what is falsey. Different languages make different decisions.

In Ruby only false and nil are falsey. Everything else is truthy (yes, even 0 is truthy).

Become familiar with the following chart:

Value Truthy?
0 yes
"" yes
[] yes
"hello" yes
nil no
6.7 yes
true yes
TRUE yes
false no
FALSE no
[1,2] yes
{:hi=>"there" yes

You get the idea!

Top-Tip: Even an empty string, "", is truthy! This might seem kind of strange, but it will make more sense once we learn more about object orientation. For now, keep in mind that even an empty string is an instance of the String class. Again, that sentence might not mean a lot to you right now, but it will soon...

Bonus: Determining Truthiness

If you forget to memorize this handy chart, there is a trick you can use to determine if a value is truthy or falsey. A single bang operator, !, will negate the boolean value it is placed in front of. For example:

!true  #=> false

and

!false #=> true

The double bang operator: A "double-bang operator" (!!) will return true or false based on whether a value is truthy or falsey to begin with.

For example:

!!"hello" #=> true

!!nil #=> false

Resources

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us