Skip to content

Instantly share code, notes, and snippets.

@ignacio
Created May 16, 2011 16:25
Show Gist options
  • Save ignacio/974770 to your computer and use it in GitHub Desktop.
Save ignacio/974770 to your computer and use it in GitHub Desktop.
Redis table with __index metamethod
local fail_safe = {
__index = function(t, command)
error( ("No command defined for %q"):format(tostring(command)) )
end
}
setmetatable(fail_safe, fail_safe)
-- You add commands here (internally)
local commands = {
get = function(t, key)
return 42
end,
set = function(t, key, value)
end
}
setmetatable(commands, fail_safe)
local redis = setmetatable({}, {
__index = commands
})
print( redis.get("x") )
-- a non existing command
redis.foo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment