Ruby’s method aliases are pretty handy. So is inheritance. It’s too bad that the two don’t work well together:
Wat? Turns out that alias_method creates an alias that references the original method rather than the overwritten one. Fortunately, the Ruby standard library provides a workaround. By using the Forwardable module and its def_delegator method, we can declare a delegator that forwards any call on to the overwritten method.
Cool, now we have the alias working with inheritance. The only problem with this approach is maintainence. Let’s make it clear why we’re using something other than alias_method by defining our own inheritable version.
Now we can clean our code up.
Now there’s clarity around why we’re not using alias_method. You can tell just by reading the method name. Rad.
The Internate is the personal website of Nate Smith. You should follow me on: