Modules Part I - Enter the Include Class
Modules in Ruby serve two purposes. One is to provide namespace scope for code organization seen in the syntax Test::Unit::TestCase, where TestCase is a class defined inside of the Unit module, which is in turn defined within the Test module. The second purpose of modules is to provide a method for code reuse which falls somewhere between abstract base classes and multiple inheritance. This post explores the implementation and consequences of this second purpose.
A module which defines code that will be shared by other modules or classes is known as a Mixin. Mixing a module into a class is generally done with a call to the include method.
A call to the ancestors of NeedALittleHelp will confirm that Useful has been inserted into NeedALittleHelp’s inheritance chain
The inheritance chain might be drawn as…

An included module will always be inserted into the inheritance chain below a declared super class.
It’s worth pointing out that a single inheritance chain has been preserved. The include method has simply manipulated Klass’s inheritance chain by inserting the MixinModule module between the class and it’s super…

It’s possible to mix multiple modules into a class as below.
Once again the Klass’s inheritance chain has been manipulated so that the super of Klass now points to AnotherMixin whose super points to MixinModule…

This raises the question of what occurs when these modules are included into a new class in reverse order…
Here the super of MixinModule is pointing to AnotherMixin. This is odd behavior…

It would seem that the super pointer of a module can point to different places depending on context. Clearly include cannot be altering the super pointer of the actual module it acts on, as the order in which a class includes the module can result in differing inheritance chains. At the same time, it’s convenient for there to be only once actual instance of a module to allow for minimal overhead in updating the module’s method table. Ruby solves this problem with something called an Include Class. When a module is included into a class (or module), a new class is created on the fly. The method table of this class is a pointer to the included modules method table. This ensures that each class that includes a module has a “copy” of the module who’s super pointer is free for manipulation. Since the method table is a pointer to the one true method table for the module, updates to the modules methods are shared between all “copies” of the module. This module “copy” is an Include Class.
The code below is a simplified version of the C function for creating a new include class.
As described above a new object is created of type T_ICLASS which is the C struct for storing include classes. A pointer to the method table of of the module is copied to the include class. On the next line the super for the include class is set to the super class passed into the function as a parameter. The class pointer for the new class is set to the module which has been copied. This reference to the actual module is is useful for the case, as in the ancestor method call above, where the interpreter needs to know the module associated with the include class. This does mean that, unlike other classes, the class of an include class is not a metaclass. We’ll discuss the implications of this when looking at including class methods. A better diagram of the NeedALittleHelp inheritance chain might look like this…

The next post will continue to explore modules and includes class. We’ll dig deeper into implementation and cover some of the nuances of inheritance chain manipulation. As always thanks for reading and feel free to leave comments with questions, corrections, or haiku.
November 25th, 2007 at 11:06 pm
I’m relatively new to Ruby, so sorry if this question is trivial.
What happens if two modules both provide a definition for method with the same name? Since they are mixed-in in a linear order, I would assume that the first one found is invoked. I would also assume that, if this were a problem, the methods in each mixin would be prefixed by something unique (like the module name) to avoid the method-name clash.
If my understanding of mixins is correct; do people find this a problem in practice?
November 26th, 2007 at 4:43 am
Hi Bill,
You are correct in assuming that method dispatch will find the method it’s looking for in the last module mixed in. There is no name collision problem as the methods are not stored in the same place, they are keys on different m_tbls. Since the method is found in the m_tbl of the latter module, the interpreter does not follow the super chain to check the m_tbl of the first module.
This behavior is more or less identical to an overridden instance method in a subclass. One of the things I’m hoping to get across in this and the following post is that mixins, positive press notwithstanding, are a form of inheritance, albeit a very dynamic one. That means that they are prey to some of the problems of deep and complex inheritance structures.
Thanks for the comment and for reading.
December 17th, 2007 at 11:29 am
Oh, no. We’re not headed towards a discussion on Traits, are we? ;)
BTW, minor typo. You’ve got a double “is” at “This reference to the actual module is is useful for the case”
December 17th, 2007 at 11:55 am
Along the same lines, what are your thoughts on selector namespaces? They could be added in Ruby to allow the user to select the inheritence hierarchy, couldn’t they?
I wrote a post about them a while back if you’re interested:
http://djberg96.livejournal.com/50830.html