Type `2 + 2` into irb and it evaluates the expression and displays `4`. An expression produces a value: even `days = 365` evaluates to `365`, the value on the assignment’s right-hand side. irb displays that result without a `print` or `puts` call. section 1.4.2
An expression can span lines
Enter `2 +` in irb. Instead of evaluating it immediately, irb prompts for the missing part. Enter `3` on the next line and the completed expression evaluates to `5`. section 1.1.2
Every Ruby expression evaluates to an object. The object produced and its Boolean value are separate questions. For example, `0` evaluates to `0`, which passes an `if` test; `nil` evaluates to `nil`, which does not. Only `false` and `nil` have a false Boolean value. section 7.5 section 7.5.2
The same distinction applies beyond arithmetic:
- `puts "Hello"` prints a line but evaluates to `nil`. irb may therefore show both the printed line and `=> nil`. section 1.4.2
- An empty class definition evaluates to `nil`; a class definition containing `1` evaluates to `1`, its last expression. section 7.5.1
- A method definition evaluates to a symbol naming the method, even if calling that method would return `false`. section 7.5.1
Output does not make a condition true
In `if puts "You'll see this"`, the message appears, but the body of the `if` does not run: `puts` returns `nil`, so its expression fails the test. section 2.2.4
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 sample expression’s resulting object with its separate Boolean value. section 7.5.2










