# Alias method in module

**URL:** <https://rubytalk.org/t/alias-method-in-module/61620>\
**Category:** ruby-talk\
**Created:** [28 January 2011 14:45 UTC](https://rubytalk.org/t/alias-method-in-module/61620 "2011-01-28T14:45:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dagnan](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@Dagnan](https://rubytalk.org/u/Dagnan)\
**Post date:** [28 January 2011 14:45 UTC](https://rubytalk.org/t/alias-method-in-module/61620/1 "2011-01-28T14:45:25Z")

</div>

Hi.

I'm trying to alias a method in a module and then overwriting this method (still having access to the old method).

I tried the following code that does not seem to work with Ruby 1.9.2. Do you have an idea why?

Thanks.

class A  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'bar'  
&nbsp;&nbsp;end  
end

module Foo  
&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;base.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias\_method :old\_foo, :foo #unless method\_defined?(:old\_foo)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'foobar'  
&nbsp;&nbsp;&nbsp;&nbsp;#old\_foo  
&nbsp;&nbsp;end  
end

A.send :include, Foo

a = A.new  
a.foo #=\> bar

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [29 January 2011 05:10 UTC](https://rubytalk.org/t/alias-method-in-module/61620/2 "2011-01-29T05:10:35Z")

</div>

try

class A  
def foo  
&nbsp;&nbsp;&nbsp;puts 'bar'  
end  
end

module Foo  
def self.included(base)  
&nbsp;&nbsp;&nbsp;base.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias\_method :old\_foo, :foo #unless method\_defined?(:old\_foo)  
&nbsp;&nbsp;&nbsp;end  
end

def initialize(\*args)  
&nbsp;&nbsp;self.extend Foo::Overrider  
end

module Overrider  
&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'foobar'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#old\_foo  
&nbsp;&nbsp;&nbsp;end  
end

end

A.send :include, Foo

a = A.new  
a.foo #=\> foobar  
a.old\_foo #=\>bar

not just sure if that is the best way for modifying a class instance  
methods. i'd rather just use require if possible ...

best regards -botp

> **···**
>
> On Fri, Jan 28, 2011 at 10:45 PM, Dagnan \<dagnan@gmail.com\> wrote:
> 
> > I'm trying to alias a method in a module and then overwriting this method (still having access to the old method).

---

<div class="post-metadata">

**Author:** ![Su\_Zhang1](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@Su\_Zhang1](https://rubytalk.org/u/Su_Zhang1)\
**Post date:** [31 January 2011 17:59 UTC](https://rubytalk.org/t/alias-method-in-module/61620/3 "2011-01-31T17:59:40Z")

</div>

As of Ruby 1.9, when you include a module M in a class C, an anonymous  
class is created and becomes the immediate superclass of C and subclass  
of C's original superclass. All instance methods of M will be inherited  
by C in the same old way. So it is actually M#foo who gets overridden  
(by its subclass C#foo); C#foo never gets redefined. This is how Ruby  
currently resolves name conflicts raised during mixin.

If you call `super' in C#foo, it will route to M#foo.

class A  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'bar'  
&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;end  
end

module Foo  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'foobar'  
&nbsp;&nbsp;end  
end

A.send :include, Foo

a = A.new  
a.foo # =\> bar\nfoobar

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dagnan](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@Dagnan](https://rubytalk.org/u/Dagnan)\
**Post date:** [1 February 2011 15:31 UTC](https://rubytalk.org/t/alias-method-in-module/61620/4 "2011-02-01T15:31:00Z")

</div>

Thank you all for your answers.

Remix and Prepend have not been updated since a few months. candlerb and bot Peña solution are interesting.

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [29 January 2011 10:03 UTC](https://rubytalk.org/t/alias-method-in-module/61620/5 "2011-01-29T10:03:47Z")

</div>

Looks like when you include module Foo, the definition of foo in the  
class still takes precedence over the one in the newly-included module.

This seems to work though:

class A  
&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'bar'  
&nbsp;&nbsp;end  
end

module Foo  
&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;base.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias\_method :old\_foo, :foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'foobar'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old\_foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

A.send :include, Foo

a = A.new  
a.foo

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [31 January 2011 18:06 UTC](https://rubytalk.org/t/alias-method-in-module/61620/6 "2011-01-31T18:06:12Z")

</div>

Su Zhang wrote in post #978699:

> class A  
> &nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;puts 'bar'  
> &nbsp;&nbsp;&nbsp;&nbsp;super  
> &nbsp;&nbsp;end  
> end
> 
> module Foo  
> &nbsp;&nbsp;def foo  
> &nbsp;&nbsp;&nbsp;&nbsp;puts 'foobar'  
> &nbsp;&nbsp;end  
> end
> 
> A.send :include, Foo
> 
> a = A.new  
> a.foo # =\> bar\nfoobar

It's the same in 1.8.7.

I think the OP was looking for a way to achieve the opposite behaviour:  
to import a module which would override the method in the class.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott1](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@Adam\_Prescott1](https://rubytalk.org/u/Adam_Prescott1)\
**Post date:** [1 February 2011 13:21 UTC](https://rubytalk.org/t/alias-method-in-module/61620/7 "2011-02-01T13:21:09Z")

</div>

John Mair (banisterfiend) has written some code to manipulate ancestor  
chains, two of which are Remix and Prepend:

Remix: [GitHub - banister/remix: Ruby modules re-mixed and remastered](https://github.com/banister/remix)  
Prepend: [GitHub - banister/prepend: prepends modules in front of a class; so method lookup starts with the module](https://github.com/banister/prepend)

Prepend should do exactly what you're looking for.

> **···**
>
> On Mon, Jan 31, 2011 at 6:06 PM, Brian Candler \<b.candler@pobox.com\> wrote:
> 
> > I think the OP was looking for a way to achieve the opposite behaviour:  
> > to import a module which would override the method in the class.
