A ticket availability test
A predicate can return false to report that a ticket is unavailable: `def ticket.available?; false; end`. An `if ticket.available?` test then takes its `else` branch. The question mark makes the method call read like a question; it does not itself determine the answer. section 2.2.4
`false` is a Ruby keyword and an object: the sole instance of `FalseClass`. It can be assigned to a variable or passed to a method, but cannot be used as a variable or method name. Comparisons can return it because Ruby methods return objects. section 7.5.2 section 7.5
Distinguish the object `false` from an expression that fails a conditional test. Every expression evaluates to an object, and only `false` and `nil` have a false Boolean value. Thus `0`, `""`, and other objects pass an `if` test. An expression such as `1 == 2` fails because it evaluates to `false`; `puts "hello"` fails as a condition because it returns `nil`. section 2.2.4 section 7.5.2
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; they are related, but not interchangeable. section 7.5.2
That distinction matters beyond ordinary method calls. An empty class definition evaluates to `nil` and fails an `if` test, while defining a method can pass the test even when calling that method would return `false`. Boolean arguments are possible too, but a bare `false` argument can obscure what choice it controls. section 7.5.1 section 7.5.2
