If these two methods are simply synonyms, why do people go to the trouble of writing the additional characters “_chain“?
If these two methods are simply synonyms, why do people go to the trouble
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No.
alias_methodis a standard method from Ruby.alias_method_chainis 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 themethodmethod with the new featurenew_feature, the following two code examples are equivalent:and
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: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_capitalizationand usealias_method_chainto resolve this:Now, the old
renameis calledrename_without_capitalization, andrename_with_capitalizationisrename. For example: