24

If these two methods are simply synonyms, why do people go to the trouble of writing the additional characters "_chain"?

John Owens
  • 273
  • 1
  • 3
  • 7

2 Answers2

59

No. alias_method is a standard method from Ruby. alias_method_chain is a Rails add-on designed to simplify the common action of aliasing the old method to a new name and then aliasing a new method to the original name. So, if for example you are creating a new version of the method method with the new feature new_feature, the following two code examples are equivalent:

alias_method :method_without_new_feature, :method
alias_method :method, :method_with_new_feature

and

alias_method_chain :method, :new_feature

EDIT

Here is a hypothetical example: suppose we had a Person class with a method rename. All it does is take a string like "John Doe", split on the space, and assign parts to first_name and last_name. For example:

person.rename("Steve Jones")
person.first_name  #=> Steve
person.last_name   #=> Jones

Now we're having a problem. We keep getting new names that aren't capitalized properly. So we can write a new method rename_with_capitalization and use alias_method_chain to resolve this:

class Person
  def rename_with_capitalization(name)
    rename_without_capitalization(name)
    self.first_name[0,1] = self.first_name[0,1].upcase
    self.last_name[0,1] = self.last_name[0,1].upcase
  end

  alias_method_chain :rename, :capitalization
end

Now, the old rename is called rename_without_capitalization, and rename_with_capitalization is rename. For example:

person.rename("bob smith")
person.first_name  #=> Bob
person.last_name   #=> Smith

person.rename_without_capitalization("tom johnson")
person.first_name  #=> tom
person.last_name   #=> johnson
Pesto
  • 23,810
  • 2
  • 71
  • 76
  • With "alias_method_chain :method, :new_feature", how do you refer to :method_without_new_feature? :method becomes an alias for :new_feature, so it seems you lose the ability to refer to :method_without_new_feature? – John Owens Jun 23 '09 at 19:37
  • 1
    alias_method_chain takes two arguments which are combined to create the resulting method names. It does not map one to the other. – tadman Jun 23 '09 at 19:42
  • @John Owens: See the added example. – Pesto Jun 23 '09 at 20:25
  • Thanks for all the detail. I understand what you mean now. Only question I have is if the relative locations of these definitions matters in the source code file. Your Person example doesn't indicate where the 'rename' method is defined in the file relative to 'rename_with_capitalization' and "alias_method_chain :rename, :capitalization". Would it matter? Also, does alias_method_chain always use '_with' and '_without' to define the respective aliases? I assume so based on your explanation. – John Owens Jun 23 '09 at 22:09
  • While the two methods will have to have been defined before you use alias_method_chain, it doesn't matter when or where they were defined. I don't show the rename method because we are assuming it already exists. If we were writing the class from scratch, it'd be simpler to give them the "with" and "without" names to begin with, rather than bother with alias_method_chain. In other words, we are assuming that at runtime we are reopening the Person class to add an additional method. If that doesn't make sense to you, you'll need to read up on Ruby's open classes. – Pesto Jun 23 '09 at 22:47
  • Thanks. I wasn't sure if it was alias, or rename. Alias usually means both names are active. The Rails docs didn't make sense to me because if it was just an alias/link, then #method_with_new_feature would call #method, which would call #method_without_new_feature and they would end up all the same method! – Chloe Mar 28 '14 at 05:44
5

alias_method_chain is worst way of doing method call interception. If you are looking for similar techniques, do not use it outside rails.

Hemant Kumar
  • 1,988
  • 2
  • 13
  • 21