[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

alias_method code snippet doubt.

Googy

5/30/2007 8:23:00 AM

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 ?

2 Answers

Jano Svitok

5/30/2007 8:38:00 AM

0

On 5/30/07, Googy <cooldudevamsee@gmail.com> wrote:
> 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 ?

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.

Googy

5/30/2007 10:38:00 AM

0

On May 30, 1:37 pm, "Jano Svitok" <jan.svi...@gmail.com> wrote:
> On 5/30/07, Googy <cooldudevam...@gmail.com> wrote:
>
>
>
> > 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 ?
>
> 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.

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.