Syntax and Expressions

Ruby is a case-sensitive language. Apart from within identifiers and some literals, blank space and comments may be placed anywhere as needed. Line breaks are treated as blank space only when used to clearly show a line is continuing; otherwise, they are treated as phrase delimiters.

Identifiers

Example:

foobar
ruby_is_simple

An identifier in Ruby begins with an alphabetic character or underscore (_) and consists of alphanumeric characters or underscores (_). There is no limit on length.

Comments

Example:

# this is a comment line

Strings beginning with # are considered comments.

Reserved Words

The following are reserved words:

BEGIN    class    ensure   nil      self     when
END      def      false    not      super    while
alias    defined? for      or       then     yield
and      do       if       redo     true
begin    else     in       rescue   undef
break    elsif    module   retry    unless
case     end      next     return   until

Reserved words cannot be used for names of classes, variables, or the like. However, words prefixed by $ or @ are not considered reserved. Furthermore, these words can be used as method names after def, after a method-call period, and in other cases where it is clear that the word is acting as a method name.

Expressions

Example
true
(1+2)*3
foo()
if test then ok else ng end

The term "expressions" encompasses everything from variables and literals to operational forms and control structures.

A collection of these expressions makes up a Ruby program. Expressions are separated from one another with semicolons (;) or line breaks. However, a line break following a backslash is not considered a new expression boundary; instead, the expression continues on the next line.

Expressions can be grouped with parentheses.

Converted from CHM to HTML with chm2web Pro 2.85 (unicode)