From: Daniel Berger Date: 2006-05-14T07:03:53+09:00 Subject: Re: Using alias_method in mixin module Justin Collins wrote: > 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). 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