Skip to content

Instantly share code, notes, and snippets.

@stuartpb
Created June 13, 2011 18:27
Show Gist options
  • Save stuartpb/1023370 to your computer and use it in GitHub Desktop.
Save stuartpb/1023370 to your computer and use it in GitHub Desktop.
A nice environmental module style for 5.2
--takes the environment to encapsulate
local function modme(encenv)
local retmod = {}
local envmt = {}
function envmt.__index(t, k)
return retmod[k] or encenv[k]
end
function envmt.__newindex(t, k, v)
retmod[k] = v
end
end
local _ENV, retmod = modme(_ENV)
-- all environment variables created here go into the module --
-- all environment variables read here read from the module --
-- if not present, they read from the encapsulating env --
-- the returned module does not provide access to the environment --
return retmod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment