This chapter explains the structure of Ruby programs. It starts with the lexical structure, covering tokens and the characters that comprise them. Next, it covers the syntactic structure of a Ruby program, explaining how expressions, control structures, methods, classes, and so on are written as a series of tokens. Finally, the chapter describes files of Ruby code, explaining how Ruby programs can be split across multiple files and how the Ruby interpreter executes a file of Ruby code.
The Ruby interpreter parses a program as a sequence of tokens. Tokens include comments, literals, punctuation, identifiers, and keywords. This section introduces these types of tokens and also includes important information about the characters that comprise the tokens and the whitespace that separates the tokens.
Comments in Ruby begin with a
# character and
continue to the end of the line. The Ruby interpreter ignores the
# character and any text that
follows it (but does not ignore the newline character, which is
meaningful whitespace and may serve as a statement terminator). If a
# character appears within a string
or regular expression literal (see Chapter 3), then
it is simply part of the string or regular expression and does not
introduce a comment:
# This entire line is a comment x = "#This is a string" # And this ...