liveBook · Manning

Boolean

As explained in The Well-Grounded Rubyist, Fourth Edition

A truth value is the Boolean interpretation Ruby assigns to an object when it is used as a condition.

A ticket as a yes-or-no question

A method named `available?` can return `true` or `false`, letting `if ticket.available?` choose between an available-seat message and a sold-seat message. The question mark makes the method call read like a question; the condition tests the value it returns. section 2.2.4

Every Ruby expression evaluates to an object, and every object has a Boolean truth value when used as a condition. Only `false` and `nil` have a false truth value. Numbers—including `0`—strings—including `""`—and other objects pass an `if` test. An object being true in this sense does not mean it is the object `true`. section 7.5 section 7.5.3

The distinction matters when an expression produces a result other than itself. `100 > 50` evaluates to `true`, while `100 < 50` evaluates to `false`. `puts "string"` prints the string but returns `nil`, so it fails as a condition. An empty class definition also evaluates to `nil`; a class definition containing `1` evaluates to `1` and passes. Defining a method evaluates to its name as a symbol, even if calling that method would return `false`. section 7.5.1 section 7.5.2 section 7.5.3

Table 7.3 Mapping sample expressions to their evaluation results and Boolean values

Expression

Object to which expression evaluates

Boolean value
of expression

1

1

True

0

0

True

1+1

2

True

true

true

True

false

false

False

nil

nil

False

"string"

"string"

True

""

""

True

puts "string"

nil

False

100 > 50

true

True

x = 10

10

True

def x; end

:x

True

class C; end

nil

False

class C; 1; end

1

True

Compare each expression’s resulting object with its Boolean value: `0` and `""` are true in conditions, whereas `puts "string"` produces false-valued `nil`. section 7.5.2

`true` and `false` are objects as well as Ruby keywords: they can be assigned to variables and passed to methods, but cannot be used as variable or method names. Each is the sole instance of its respective class, `TrueClass` or `FalseClass`. A Boolean method argument can be useful, but avoid one when its meaning would be hard to remember. section 7.5.2

`nil` is an object that represents absence, not another spelling of `false`. An uninitialized instance variable or an array access beyond its elements can produce `nil`. Both `nil` and `false` fail a conditional test, but keeping their distinct meanings in mind makes the result easier to interpret. section 7.5.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Distinguishes an expression’s resulting object from its truth value through worked conditional tests and a comparison table.
Examples
Tests ticket availability, zero, empty strings, class and method definitions, comparisons, and `puts` return values.
What it assumes
Assumes familiarity with methods and `if`, while building the truth-value distinction from small expression tests.
What you can do after
Evaluate/understand why a Ruby condition passes or fails without mistaking a truth value for the object `true`.
How it connects
Connects Boolean conditions to expression evaluation, comparison methods, method return values, and nil.

Also explained in

Compare how 11 books explain boolean →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.