Forum: Ruby Using alias_method in mixin module

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F3b7109c91841c7106784d229418f5dd?d=identicon&s=25 Justin Collins (justincollins)
on 2006-05-13 23:18
(Received via mailing list)
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) {
                            #use old_look_inside in here


Is there a better/different way of doing this? (This works, I'm just

Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2006-05-14 00:06
(Received via mailing list)
Justin Collins 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

class Foo
    include Example, :alias => {:look_inside, :example_look_inside}

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.


F3b7109c91841c7106784d229418f5dd?d=identicon&s=25 Justin Collins (justincollins)
on 2006-05-14 00:50
(Received via mailing list)
Daniel Berger 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.


This topic is locked and can not be replied to.