Skip to content

Instantly share code, notes, and snippets.

@hoelzro
Created October 19, 2011 21:07
Show Gist options
  • Save hoelzro/1299679 to your computer and use it in GitHub Desktop.
Save hoelzro/1299679 to your computer and use it in GitHub Desktop.
Simple relative require in Lua
require 'baz'
print "I'm in baz!"
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