Avoiding alias_method_chain in Ruby
09 Apr 2009I currently use Ruby a lot but since I am not into developing web applications, I am not knowledgeable about Ruby on Rails. So when I watched Yehuda’s presentation about refactoring Rails I watched it not so much because of Rails but because of refactoring Ruby software.
Yehuda talks about introducing modules to be able to use super instead of alias_method_chain. I tried it out on an example and it seems to be a really useful thing to know.
Using alias_method_chain
This approach uses the popular definition of alias_method_chain
As an example we have a class Test with a method x
Using alias_method_chain the method x can be extended without having to introduce a new inheriting class
The method now exhibits the new behaviour
Using modules
However the method x can be declared in a module which is included by Test
Overloading the method now can be done by including another module which has a new definition of x. The new method x can invoke the previous one by calling super
The method now exhibits the new behaviour
The issue with this approach is that overloading of the method needs to be anticipated. However at the same time the code indicates it more clearly that the first definition of x is not necessarily the final one in objects of type Test. Moreover this approach works without public definitions of methods you are not supposed to call (e.g. x_without_two).
Update: If you like to see another talk from MountainWest RubyConf 2009, you should check out Jim Weirich’s talk on The Building Blocks of Modularity.
See also:
Update: More recent post by Yehuda Katz
Update: Ruby 2.0 will introduce Module#prepend