Bo

Bools in Crystal

1 exercise

About Bools

Crystal has a type known as Bool. It represents the values true and false.

Logical operators

Crystal has three logical operators (!, ||, &&), which combine Bools and make expressions that produce different values.

And(&&)

The and operator in Crystal is represented by && and returns true if both values are true; otherwise, it returns false. When using the and operator, one Bool is placed on the right side of the && and another on the left side.

true && true
# => true

true && false
# => false

Or(||)

The or operator in Crystal is represented by || and returns true if at least one of the values given is true. If both of the values are false, then it returns false. When using the or operator, one Bool should be placed on the right side of the || and another on the left.

true || true
# => true

true || false
# => true

false || false
# => false

Not(!)

The not operator in Crystal is represented by ! and returns true if the given Bool is false and returns false if true is provided. When using the not operator, one Bool should be placed after the operator (!).

!true
# => false

!false
# => true

Using parentheses(())

When working with booleans, you can use parentheses to decide which Bools to evaluate first. The result can differ depending on how the parentheses are used. In Crystal, what is in parentheses is evaluated first.

true && false && false || true
# => true

true && false && (false || true)
# => false

Since what is in parentheses is evaluated first, the not operator will apply to the expression inside parentheses in the following example.

!true && false
# => false

!(true && false)
# => true
Note

You should only use parentheses when they affect the result; otherwise, they should be omitted.

Truthy and falsey

In Crystal, the "falsey" values are: false, nil and null pointers; everything else is "truthy". These values can be used with the logical operators mentioned above.


true && nil
# => false

false && 4
# => false

true || nil
# => true

Method naming conventions with booleans

In Crystal, it is common to name a method that returns a truthy or falsey value with a question mark (?). In most cases, this method will return a Bool value, but it doesn't have to. This means that the method can be used in places where a truthy or falsy value is expected, for instance, as a condition for an if statement:

def exchange(value)
    if value.nil?
        value = 5
    end
    value
end

exchange(10)
# => 10

exchange(nil)
# => 5

Methods with a question mark at the end of their name that don't return a Bool value, often return nil instead. nil is similar to None or null from other languages.

Edit via GitHub The link opens in a new window or tab

Learn Bools

Practicing is locked

Unlock 9 more exercises to practice Bools