In the following code from ruby docs, why doesn’t orig_exit end up calling itself in infinite recursion?
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)
Because there is no recursion here.
First,
exitis called, from the last line (exit(99)) which in turn callsorig_exit, which is a different function. Unlessorig_exitexplicitly callsexit(which there is no reason to believe it does) there is no possibility for recursion. Whenorig_exitreturns, its return value is returned fromexitas well.alias_methodhas renamed the method that was namedexittoorig_exit, and then a completely new function namedexitis defined.