Ruby Internals, by Patrick Farley

I’ve watched the presentation Ruby Internals, from Patrick Farley, and I thought it was really interesting.


Here are my own highlights:

  • The building blocks of MRI’s implementation are C structs: RObject, RBasic, RClass.
  • instance methods (behavior) in a Ruby class are stored in a C struct called RClass (and not in RObject)
  • method dispatch will always work the same way in MRI no matter what, no exceptions:
    1. a message is being sent to an object
    2. the class pointer to that object is being dereferenced
    3. the interpreter is looking in the class’ methods table hash
    4. if cannot find the method, it calls the super pointer and looks in the methods table hash there
    5. it repeats (4) until it finds a null (in which case we have a method_missing exception)

  • Definitions of “singleton class” and “metaclass” (according to the Pick Axe)
    • 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.
  • Knowing that ‘extends’ actually calls ‘include’ on the singleton of the extended object:
    • 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)
  • 2 widely-used perceptions of a mixin (it’s actually neither, but closer to the second one)
    • 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.

Advertisements

Got a comment?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: