O'Reilly logo

Ruby on Rails® Bible by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required