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 |
|---|---|---|
|
|
|
True |
|
|
|
True |
|
|
|
True |
|
|
|
True |
|
|
|
False |
|
|
|
False |
|
|
|
True |
|
|
|
True |
|
|
|
False |
|
|
|
True |
|
|
|
True |
|
|
|
True |
|
|
|
False |
|
|
|
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










