Program Execution
Ruby is a scripting language. This means that Ruby programs are simply lists, or scripts, of statements to be executed. By default, these statements are executed sequentially, in the order they appear. Ruby’s control structures (described in Chapter 5) alter this default execution order and allow statements to be executed conditionally or repeatedly, for example.
Programmers who are used to traditional static compiled languages like C or Java may find this slightly
confusing. There is no special main
method in Ruby from which execution begins. The Ruby interpreter
is given a script of statements to execute, and it begins executing at
the first line and continues to the last line.
(Actually, that last statement is not quite true. The Ruby
interpreter first scans the file for BEGIN statements,
and executes the code in their bodies. Then it goes back to line 1 and
starts executing sequentially. See BEGIN and END for more
on BEGIN.)
Another difference between Ruby and compiled languages has to do with module, class, and method definitions. In compiled languages, these are syntactic structures that are processed by the compiler. In Ruby, they are statements like any other. When the Ruby interpreter encounters a class definition, it executes it, causing a new class to come into existence. Similarly, when the Ruby interpreter encounters a method definition, it executes it, causing a new method to be defined. Later in the program, the interpreter will probably encounter and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access