Ruby treats every method body as a sequence of expressions. The method’s return value is the value produced by the last expression evaluated, so an explicit return is often unnecessary. For example, a temperature-conversion calculation can be the method’s only expression and becomes its result. section 2.1.4
A getter returns its instance variable
def venue @venue end The expression @venue is the final—and only—expression in venue, so calling venue returns the stored venue. An explicit return can make the result more visible, return from the middle of a method, or return multiple values; an empty method body returns nil. section 3.2.1 section 2.1.4
Table 2.1 Examples of Ruby expressions and the values to which they evaluate
| Expression |
Value |
Comments |
|---|---|---|
|
|
|
Arithmetic expressions evaluate to their results. |
|
|
|
A simple, literal string (in quotation marks) evaluates to itself. |
|
|
|
Strings can be “added” to each other (concatenated) with the plus sign. |
|
|
|
When you assign to a variable, the whole assignment evaluates to the value you’ve assigned. |
|
|
|
The usual rules of precedence apply: multiplication and division bind more tightly than addition and are performed first. |
|
|
|
A method call is an expression. |
Use the expression table to compare arithmetic, literals, assignments, and method calls: each evaluates to a value that can become a later expression’s input or a method’s result. section 2.1.4
