Appendix A. Ruby Quick Reference

Basic Ruby Syntax

The Ruby language is made up of expressions. Each expression returns a value. Even elements that are just statements in other languages, such as if and for, are expressions in Ruby that return values.

Ruby is a pure object-oriented language. Every variable or constant in Ruby is an object, and there are no basic non-object types. Every variable or literal responds to the basic method call syntax.

A simple Ruby expression is one of the following:

  • A literal. Ruby has literal syntax for arrays, hashes, numbers, ranges, regular expressions, strings, and symbols.

  • The name of an existing variable or constant.

  • A method call, which combines the name of an existing variable or constant with a method name. The basic form of a method call is <receiver>.<method>(<arguments>). Variants on this form will be discussed later.

  • One of several special expressions invoked by the use of a keyword such as if, case, or while.

Complex expressions can be built using Ruby operators. Variable assignment using = is considered to be a type of operator. Most expressions can also have arbitrarily complex expressions within them — for example, the arguments of a method call are all themselves expressions.

A Ruby expression ends with a line break unless the Ruby interpreter has a reason to believe the expression is intended to continue. The expression continues if there is an open delimiter such as a quotation mark, parenthesis, bracket, or brace. The expression also continues ...

Get Ruby on Rails® Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.