Forum: Ruby alias_method code snippet doubt.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3f4a3ca5891cfc2ca33c52e7f25cdfc9?d=identicon&s=25 Googy (Guest)
on 2007-05-30 10:25
(Received via mailing list)
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 ?
97550977337c9f0a0e1a9553e55bfaa0?d=identicon&s=25 Jano Svitok (Guest)
on 2007-05-30 10:38
(Received via mailing list)
On 5/30/07, Googy <cooldudevamsee@gmail.com> 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.
3f4a3ca5891cfc2ca33c52e7f25cdfc9?d=identicon&s=25 Googy (Guest)
on 2007-05-30 12:41
(Received via mailing list)
On May 30, 1:37 pm, "Jano Svitok" <jan.svi...@gmail.com> 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.
This topic is locked and can not be replied to.