# Using alias\_method in mixin module

**URL:** <https://www.ruby-forum.com/t/using-alias-method-in-mixin-module/60448>\
**Category:** Ruby\
**Created:** [May 13, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/using-alias-method-in-mixin-module/60448 "2006-05-13T23:18:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [May 13, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/using-alias-method-in-mixin-module/60448/1 "2006-05-13T23:18:22Z")

</div>

Hi all,

I have a mixin module that needs to redefine a particular method if it  
already exists and retain a reference to it for later use. This is what  
I came up with:

module Example

```
    def Example.included(klass)
            if klass.instance_methods.include? "look_inside"
                    klass.instance_eval { alias_method

```

:old\_look\_inside, :look\_inside }  
klass.send(:define\_method, :look\_inside) {  
|event|  
#use old\_look\_inside in here  
}  
end  
end

end

Is there a better/different way of doing this? (This works, I’m just  
curious).

-Justin

---

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [May 14, 2006, 12:06am UTC](https://www.ruby-forum.com/t/using-alias-method-in-mixin-module/60448/2 "2006-05-14T00:06:54Z")

</div>

Justin C. wrote:

> ```
> klass.instance_eval { alias_method 
> 
> ```
> 
> Is there a better/different way of doing this? (This works, I’m just  
> curious).

I’d say you should alias the module’s method, not the original class’  
method, because otherwise class hierarchy issues could get confusing.  
How should a subclass behave, for example?

Anyway, I wrote the ‘use’ package specifically to deal with this  
situation:

class Foo  
include Example, :alias =\> {:look\_inside, :example\_look\_inside}  
end

Your other option is to adopt a simple naming convention for your  
modules, where all methods from the Example module start with ‘example\_’  
(for example), and avoid the entire issue altogether.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![justincollins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5ac83/32.png) [@justincollins](https://www.ruby-forum.com/u/justincollins)\
**Post date:** [May 14, 2006, 12:50am UTC](https://www.ruby-forum.com/t/using-alias-method-in-mixin-module/60448/3 "2006-05-14T00:50:01Z")

</div>

Daniel B. wrote:

> > ```
> > if klass.instance_methods.include? "look_inside"
> > 
> > ```
> 
> Dan  
> Hi Dan,

The module should only provide this new method if the class that is  
including it has it. In other words, I need to wrap the method call in  
some logic when this module is included. The module does not provide the  
method if the class that mixes in the module does not have the existing  
method. I haven’t seen a way of doing the alias in the module that will  
accomplish this. I’m not sure using a naming convention would help in  
this particular case, because it should be transparent to any outside  
calls to the method.  
As far as subclassing (subclassing the class which mixes in the  
module?), it should retain the behavior provided in the module.

Thanks.

-Justin
