I’ve watched the presentation Ruby Internals, from Patrick Farley, and I thought it was really interesting.
- The building blocks of MRI’s implementation are C structs:
- instance methods (behavior) in a Ruby class are stored in a C struct called
RClass(and not in
- method dispatch will always work the same way in MRI no matter what, no exceptions:
- a message is being sent to an object
- the class pointer to that object is being dereferenced
- the interpreter is looking in the class’ methods table hash
- if cannot find the method, it calls the
superpointer and looks in the methods table hash there
- it repeats (4) until it finds a null (in which case we have a
- a singleton class is an instance-specific class, spawn up at runtime, when you’re trying to add instance-specific behavior to an object (a class instance)
- a metaclass is a class of a class. A metaclass is a special case of the singleton class: it’s a singleton class for a class. As soon as you define a class, you have a metaclass.
- calling ‘extends’ on a class instance will add instance-specific methods (because ‘extends’ internally calls ‘include’ on the singleton class of the instance)
- calling ‘extends’ in a class A will add class methods to class A (because ‘extends’ internally calls ‘include’ on the singleton class of class A, which is A’s metaclass)
- feature for multiple inheritance
- abstract base class (closer to this, since the module becomes a superclass of the included class)
And note part, I found the presenter, Patrick Farley, a top speaker. He’s fast-paced, dense, yet very clear and entertaining.