public Method

Module.alias_method(p1, p2)

alias_method(new_name, old_name)    self

Makes new_name a new copy of the method old_name. This can be used to retain access to methods that are overridden.

module Mod
  alias_method :orig_exit, :exit
  def exit(code=0)
    puts "Exiting with code #{code}"
    orig_exit(code)
  end
end
include Mod
exit(99)

produces:

Exiting with code 99

Source Code

/*
*  call-seq:
*     alias_method(new_name, old_name)   => self
*  
*  Makes <i>new_name</i> a new copy of the method <i>old_name</i>. This can
*  be used to retain access to methods that are overridden.
*     
*     module Mod
*       alias_method :orig_exit, :exit
*       def exit(code=0)
*         puts "Exiting with code #{code}"
*         orig_exit(code)
*       end
*     end
*     include Mod
*     exit(99)
*     
*  <em>produces:</em>
*     
*     Exiting with code 99
*/

static VALUE
rb_mod_alias_method(mod, newname, oldname)
   VALUE mod, newname, oldname;
{
   rb_alias(mod, rb_to_id(newname), rb_to_id(oldname));
   return mod;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.