Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>No. <code>alias_method</code> is a standard method from Ruby. <code>alias_method_chain</code> 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 <code>method</code> method with the new feature <code>new_feature</code>, the following two code examples are equivalent:</p> <pre><code>alias_method :method_without_new_feature, :method alias_method :method, :method_with_new_feature </code></pre> <p>and</p> <pre><code>alias_method_chain :method, :new_feature </code></pre> <hr> <h2>EDIT</h2> <p>Here is a hypothetical example: suppose we had a Person class with a method <code>rename</code>. 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:</p> <pre><code>person.rename("Steve Jones") person.first_name #=&gt; Steve person.last_name #=&gt; Jones </code></pre> <p>Now we're having a problem. We keep getting new names that aren't capitalized properly. So we can write a new method <code>rename_with_capitalization</code> and use <code>alias_method_chain</code> to resolve this:</p> <pre><code>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 </code></pre> <p>Now, the old <code>rename</code> is called <code>rename_without_capitalization</code>, and <code>rename_with_capitalization</code> is <code>rename</code>. For example:</p> <pre><code>person.rename("bob smith") person.first_name #=&gt; Bob person.last_name #=&gt; Smith person.rename_without_capitalization("tom johnson") person.first_name #=&gt; tom person.last_name #=&gt; johnson </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload