In a previous Ruby Magic, we figured out how to reliably inject modules into classes by overwriting its .new
method, allowing us to wrap methods with additional behavior.
This time, we're taking it one step further by extracting that behaviour into a module of its own so we can reuse it. We'll build a Wrappable
module that handles the class extension for us, and we'll learn all about class-level instance variables along the way. Let's dive right in!
Introducing the Wrappable
Module
In order to wrap objects with modules when they are initialized, we have to let the class know what wrapping models to use. Let’s start by creating a simple Wrappable
module that provides a wrap
method which pushes the given module into an array defined as a class attribute. Additionally, we redefine the new
method as discussed in the previous post.
To add the new behavior to a class, we use extend
. The extend
method adds the given module to the class. The methods then become class methods. To add a module to wrap instances of this class with, we can now call the wrap
method.
Let’s give this a try by creating a new instance of Bird
and calling the make_noise
method.
Great! It works as expected. However, things start to behave a bit strange once we extend a second class with the Wrappable
module.
Even though Machine
hasn't been wrapped with the Logging
module, it still outputs logging information. What’s worse - even the bird is now powering up and down. That can’t be right, can it?
The root of this problem lies in the way we are storing the modules. The class variable @@wrappables
is defined on the Wrappable
module and used whenever we add a new module, regardless of the class that wrap
is used in.
This get’s more obvious when looking at the class variables defined on the Wrappable
module and the Bird
and Machine
classes. While Wrappable
has a class method defined, the two classes don't.
To fix this, we have to modify the implementation so that it uses instance variables. However, these aren't variables on the instances of Bird
or Machine
, but instance variables on the classes themselves.
In Ruby, classes are just objects
This is definitely a bit mind boggling at first, but still a very important
concept to understand. Classes are instances of Class
and
writing class Bird; end
is equivalent to writing
Bird = Class.new
. To make things even more confusing
Class
inherits from Module
which inherits from
Object
. As a result, classes and modules have the same methods
as any other object. Most of the methods we use on classes (like the
attr_accessor
macro) are actually instance methods of
Module
.
Using Instance Variables on Classes
Let’s change the Wrappable
implementation to use instance variables. To keep things a bit cleaner, we introduce a wrappers
method that either sets up the array or returns the existing one when the instance variable already exists. We also modify the wrap
and new
methods so that they utilize that new method.
When we check the instance variables on the module and on the two classes, we can see that both Bird
and Machine
now maintain their own collection of wrapping modules.
Not surprisingly, this also fixes the problem we observed earlier - now, both classes are wrapped with their own individual modules.
Supporting Inheritance
This all works great until inheritance is introduced. We would expect that classes would inherit the wrapping modules from the superclass. Let’s check if that's the case.
As you can see, it doesn’t work as expected, because Pigeon
is also maintaining its own collection of wrapping modules. While it makes sense that wrapping modules defined for Pigeon
aren’t defined on Bird
, it’s not exactly what we want. Let’s figure out a way to get all wrappers from the entire inheritance chain.
Lucky for us, Ruby provides the Module#ancestors
method to list all the classes and modules a class (or module) inherits from.
By adding a grep
call, we can pick the ones that are actually extended with Wrappable
. As we want to wrap the instances with wrappers from higher up the chain first, we call .reverse
to flip the order.
Ruby’s #===
method
Some of Ruby’s magic comes down to the #===
(or
case equality) method. By default, it behaves just like the
#==
(or equality) method. However, several classes
override the #===
method to provide different behavior in
case
statements. This is how you can use regular expressions (
#===
is equivalent to #match?
), or classes (
#===
is equivalent to #kind_of?
) in those
statements. Methods like Enumerable#grep
,
Enumerable#all?
, or Enumerable#any?
also rely on
the case equality method.
Now we can call flat_map(&:wrappers)
to get a list of all wrappers defined in the inheritance chain as a single array.
All that's left is packing that into an inherited_wrappers
module and slightly modifying the new method so that it uses that instead of the wrappers
method.
A final test run confirms that everything is now working as expected. The wrapping modules are only applied to the class (and its subclasses) they are applied on.
That's a wrap!
Admittedly, these noisy birds are a bit of a theoretic example (tweet, tweet). But inheritable class instance variables are not just cool to understand how classes work. They are a great example that classes are just objects in Ruby.
And we'll admit that inheritable class instance variables might even be quite useful in real life. For example, think about defining attributes and relationships on a model with the ability to introspect them later. For us the magic is to play around with this and get a better understanding of how things work. And open your mind for a next level of solutions. 🧙🏼♀️
As always, we’re looking forward to hearing what you build using this or similar patterns. Just chirp to @AppSignal on Twitter.