Phew, seems like a long time since I introduced Ruby as "the object-oriented scripting language," eh? But now you have everything you need to get the nitty-gritty details on how Ruby treats classes and objects. After you've mastered a few concepts and Ruby's syntax for dealing with objects, you may never want to go back to your old languages, so beware!
All Ruby data consists of objects that are
instances of some class. Even a class itself is an object that is an
instance of the
Class class. As a general rule,
new instances are created using the
new method of
a class, but there are some exceptions (such as the
a = Array::new s = String::new o = Object::new
Class methods are defined with the
def statement. The
statement adds a method to the innermost class or module definition
def statement. A
def statement outside a class or module definition
(at the top level) adds a method to the
class itself, thus defining a method that can be referenced anywhere
in the program.
When a method is called, Ruby searches for it in a number of places in the following order:
Among the methods defined in that object (i.e., singleton methods).
Among the methods defined by that object's class.
Among the methods of the modules included by that class.
Among the methods of the superclass.
Among the methods of the modules included by that superclass.
Repeats Steps 4 and 5 until the top-level object is reached.