# Alias\_method code snippet doubt

**URL:** <https://www.ruby-forum.com/t/alias-method-code-snippet-doubt/105471>\
**Category:** Ruby\
**Created:** [May 30, 2007, 10:25am UTC](https://www.ruby-forum.com/t/alias-method-code-snippet-doubt/105471 "2007-05-30T10:25:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Googy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@Googy](https://www.ruby-forum.com/u/Googy)\
**Post date:** [May 30, 2007, 10:25am UTC](https://www.ruby-forum.com/t/alias-method-code-snippet-doubt/105471/1 "2007-05-30T10:25:44Z")

</div>

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)

Above snippet we are creating new alias (orig\_exit) for already  
existing method exit. Interesting thing is orig\_exit is again called  
in exit method, why won’t this behavior cause recursive calling of  
method it self since orig\_exit is simply alias to exit method ?

---

<div class="post-metadata">

**Author:** ![Googy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@Googy](https://www.ruby-forum.com/u/Googy)\
**Post date:** [May 30, 2007, 10:38am UTC](https://www.ruby-forum.com/t/alias-method-code-snippet-doubt/105471/2 "2007-05-30T10:38:46Z")

</div>

On 5/30/07, Googy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Above snippet we are creating new alias (orig\_exit) for already  
> existing method exit. Interesting thing is orig\_exit is again called  
> in exit method, why won’t this behavior cause recursive calling of  
> method it self since orig\_exit is simply alias to exit method ?

This is a pattern how to preserve original method that you want to  
override.  
Alias stores the method as it was when alias was called. When you  
override the method, the alias stil points to the old version –  
that’s the point of this construct.

J.

---

<div class="post-metadata">

**Author:** ![Googy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dfb087/32.png) [@Googy](https://www.ruby-forum.com/u/Googy)\
**Post date:** [May 30, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/alias-method-code-snippet-doubt/105471/3 "2007-05-30T12:41:29Z")

</div>

On May 30, 1:37 pm, “Jano S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > override the method, the alias stil points to the old version –  
> > that’s the point of this construct.
> 
> J.

Thanks I am now able to understand whats going on, Until now I used to  
assume like alias’ed method and original method point to single  
version of method.

Thanks for clarifying.
