I was working on my web-application and I wanted to override a method for example if the original class is
class A
  def foo
    'original'
  end
end
I want to override foo method it can be done like this
class A
  alias_method :old_foo, :foo
  def foo
    old_foo + ' and another foo'
  end
end
and I can call both old and new methods like this
obj = A.new
obj.foo  #=> 'original and another foo'
obj.old_foo #=> 'original'
so what is the use of alias_method_chain if I can access and keep both methods like the way I did ?
 
                        
alias_method_chainbehaves different thanalias_methodIf you have method
do_somethingand you want to override it, keeping the old method, you can do:which is equivalent to:
this allows us to easily override method, adding for example custom logging. Imagine a
Fooclass withdo_somethingmethod, which we want to override. We can do:So to have your job done, you can do:
Since it isn't very complicated, you can also do it with plain
alias_method:For more information, you can refer documentation.