This chapter describes the syntax constructs and general structure of Ruby programs.
As a brief overview, it can be said that:
- Ruby program consists of expressions dealing with literals, variables and constants.
- Expressions are:
- assignments;
- control expressions;
- method calls;
- definitions of modules, classes and methods.
- Ruby is an object-oriented language, so the program is structured by defining classes and modules and their methods.
- Ruby has open classes that can be changed any time (even the core ones, like
String
). To localize class changes and implement hygienic extensions, one can use refinements.
- Ruby has open classes that can be changed any time (even the core ones, like
- Error reporting and handling is done with exceptions.
Note that many of the language constructs you will see in a typical Ruby program, are in fact, just methods. For example Kernel#raise
is used to raise an exception, and Module#private
is used to change a method's visibility. As a result, the language core described in this chapter is pretty small, and everything else just follows usual rules for modules, methods and expressions.