views:

1079

answers:

2

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

+10  A: 

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
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
alias_method_chain takes two arguments which are combined to create the resulting method names. It does not map one to the other.
tadman
@John Owens: See the added example.
Pesto
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
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
+2  A: 

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
Can you explain why? :)
Brendon Muir
Well for starters:http://yehudakatz.com/2009/03/06/alias_method_chain-in-models/
Hemant Kumar