liveBook · Manning

Expression

As explained in The Well-Grounded Rubyist, Fourth Edition

A Ruby expression is syntactic code that can be evaluated to produce a value, including code continued across multiple lines.

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
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 sample expression’s resulting object with its separate Boolean value. section 7.5.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through assignment, method calls, definitions, and conditional results; does not descend into interpreter internals.
Examples
Uses irb arithmetic, multiline addition, assignment, class and method definitions, and a `puts` call inside `if`.
What it assumes
Introduces expressions through basic Ruby arithmetic and assignment in irb before examining less obvious results.
What you can do after
Diagnose/debug with it by separating an expression’s result, visible output, and behavior in a conditional test.
How it connects
Connects expressions to assignment, method return values, class definitions, Boolean values, and conditional statements.

Also explained in

Compare how 11 books explain expression →

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.