-
An object consists of a group of instance variables and a class reference.
-
Object methods exist in the class to which the object belongs (from the class perspective, they are called instance methods ).
-
The class itself is the object of the class. The class name is just a constant.
-
A class is a subclass of a module. A module is basically a package composed of a group of methods. In addition to the features of modules, classes can also be instantiated (through the new () method) and organized as hierarchies (through its super-class () method ).
-
Constants, like file systems, are organized in a tree structure. The module and class names play the role of the Directory, while other common constants play the role of the file.
-
Each class has an ancestor chain, which starts from its own class and ends up until the end of the basicobject class.
When a method is called, Ruby first goes to the class to which the receiver belongs, and then looks up the ancestor chain until the method is found or reached the top of the chain.
-
Every time a class contains a module, the module is inserted into the ancestor chain and positioned on the top of the class.
-
When a method is called, the receiver assumes the role of self.
-
When a module (or class) is defined, the module assumes the role of self.
Ruby metaprogramming: Object Model