Ryan Leavengood wrote:
This does seem better, but only if everytime someone overrides require in
an included module they be sure to call super.
Also it seems to be the alias method is fine, as long as everyone uses a
unique name for the old method:
alias :_old_require_ :require
puts 'New require, now calling _old_require_...'
alias :_some_old_require_ :require
puts 'New require, now calling _some_old_require_...'
New require, now calling _some_old_require_...
New require, now calling _old_require_...
If unique names aren't used, the result is an infinite loop.
That's what I expected too. But when I tried, it didn't work so well.
My aliased version was working fine until I installed RubyGems.
Appaerently it was running my version twice. Thus escaping twice, ""
-> "%5B%5D" -> "%255B%255D". Perhaps this is b/c RubyGems uses a rescue
clause and retries and so ends up passing the name to my routine again.
But you also make a good point about my inheritance chain version.
Someone else using the alias technique after mine would by pass mine
altogether. It works with RubyGems only b/c RubyGems loads first.
In thinking it over some, I suspect the best solution is either to
change the built-in require to support an observer-type pattern, so one
can tap into it. Or we simply need to agree on a standard, which I
imagine would be basically:
# Always available bypass.
# This should be built into ruby.
alias __require__ require
def require( path )
super( newpath ) # <----.
rescue LoadError # or vise-versa
super( path ) # <----'