alias_method_chain
behaves different than alias_method
If you have method do_something
and you want to override it, keeping the old method, you can do:
alias_method_chain :do_something, :something_else
which is equivalent to:
alias_method :do_something_without_something_else, :do_something
alias_method :do_something, :do_something_with_something_else
this allows us to easily override method, adding for example custom logging. Imagine a Foo
class with do_something
method, which we want to override. We can do:
class Foo
def do_something_with_logging(*args, &block)
result = do_something_without_logging(*args, &block)
custom_log(result)
result
end
alias_method_chain :do_something, :logging
end
So to have your job done, you can do:
class A
def foo_with_another
'another foo'
end
alias_method_chain :foo, :another
end
a = A.new
a.foo # => "another foo"
a.foo_without_another # => "original"
Since it isn't very complicated, you can also do it with plain alias_method
:
class A
def new_foo
'another foo'
end
alias_method :old_foo, :foo
alias_method :foo, :new_foo
end
a = A.new
a.foo # => "another foo"
a.old_foo # => "original"
For more information, you can refer documentation.