Such a rigorous definition exists and is defined in the Ruby Language Specification (published by the Japanese Information-technology Promotion Agency [IPA] in August, 2010).
At the core of the language are two fundamental concepts: objects and classes.
Here is a summary of their characteristics, capabilities and relationships:
- everything in Ruby is an object (almost)1, even primitives and classes are objects.
- objects have instance variables which hold that objects state (these bindings are always private).
- there is a special kind of object known as a class.
- classes (as an object) can have instance variables.
- classes (and only classes) can also have constants.
- classes (and only classes) can also have class variables.
- classes (and only classes) also have methods which determine their behavior.
- a class has one direct superclass.
- classes have an associated collection of objects called direct instances.
- direct instances have instance variables (but no other kind of variable binding)
- direct instances obtain their behavior from their class; methods are never bound to them directly.
- a class' instance variable is that class' own private state; whereas a class' class variables are state shared among the family of objects of that class (i.e. all subclasses and their direct instances).
- there is a special kind of class known as a singleton class.
- a singleton class is itself an object.
- a singleton class is always associated with some other object.
- the way this works is that the singleton class is inserted in between the object and its class. E.g. if foo is a direct instance of Foo, then when an a singleton class is associated with foo, foo becomes a direct instance of that singleton class and that singleton class is a subclass of Foo.
- In this association, then, the singleton class gets first crack at providing the behavior for that object.
- this is how direct instances appear to have methods defined on them; that behavior is provided by the singleton class.
- this is also how classes appear to have "class level methods"; these are merely methods defined on the singleton class for the given class. In fact, this case is so common, that when a class is created, it comes complete with a singleton class, already.
1 — operators, blocks, and methods are not objects; although the last two can be wrapped in objects (see also: http://stackoverflow.com/a/416154) ↩
No comments:
Post a Comment