Avoiding alias_method_chain in Ruby

I 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 Katz - The Great Rails Refactor

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

class Module
  def alias_method_chain( target, feature )
    alias_method "#{target}_without_#{feature}", target
    alias_method target, "#{target}_with_#{feature}"
  end
end

As an example we have a class Test with a method x

class Test
  def x
    'x'
  end
end

Using alias_method_chain the method x can be extended without having to introduce a new inheriting class

class Test
  def x_with_two
    "#{x_without_two}2"
  end
  alias_method_chain :x, :two
end

The method now exhibits the new behaviour

puts Test.new.x
# "x2"

Using modules

However the method x can be declared in a module which is included by Test

module X
  def x
    "x"
  end
  module_function :x
end
class Test
  include X
  public :x
end

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

module X2
  def x
    "#{super}2"
  end
  module_function :x
end
class Test
  include X2
end

The method now exhibits the new behaviour

puts Test.new.x
# "x2"

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