Created
October 19, 2011 21:07
-
-
Save hoelzro/1299679 to your computer and use it in GitHub Desktop.
Simple relative require in Lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'baz' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print "I'm in baz!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local oldrequire = require | |
function require(modname) | |
local regular_loader = package.loaders[2] | |
local loader = function(inner) | |
if string.match(modname, '(.*)%.') then | |
return regular_loader(string.match(modname, '(.*)%.') .. '.' .. inner) | |
end | |
end | |
table.insert(package.loaders, 1, loader) | |
local retval = oldrequire(modname) | |
table.remove(package.loaders, 1) | |
return retval | |
end | |
require 'foo.bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment