Ruby Programming/Syntax/Lexicology

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Identifiers

Example:

foobar
ruby_is_simple

An identifier in Ruby begins with an English letter (A-Za-z) or underscore (_) and consists of alphanumeric characters (A-Za-z0-9) or underscores (_). There is no restriction to its length. Identifiers are names used to identify variables, definitions, classes, etcetera in a program and from other other variables, definitions, …. An identifier cannot have a reserved word as its name .

[edit] Comments

Example:

# this is a comment line

Other than within a string, a comment is regarded as the text from the # to the end of the line.

[edit] Embedded Documentation

Example:

=begin
everything between a line beginning with `=begin' down to
one beginning with `=end' will be skipped by the interpreter.
=end

[edit] Reserved Words

The following words are reserved in Ruby:

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

[edit] Expressions

Example: true

(1 + 2) * 3
foo()
if test then okay else not_good end

All variables, literals, operators, control structures, etcetera are expressions. Using these together is called a program. You can divide expressions with newlines or semicolons (;) — however, a newline with a preceeding backslash (\) is continued to the following line.