So, with a hint from @lhf, I dealt with a passive double (from what I can say) method_missing . As a result, I developed the following:
local field = '__method__missing' function method_missing(selfs, func) local meta = getmetatable(selfs) local f if meta then f = meta.__index else meta = {} f = rawget end meta.__index = function(self, name) local v = f(self, name) if v then return v end rawget(self, name)[field] = function(...) return func(self, name, ...) end end setmetatable(selfs, meta) end debug.setmetatable(nil, { __call = function(self, ...) if self[field] then return self[field](...) end return nil end, __index = function(self, name) if name~=field then error("attempt to index a nil value") end return getmetatable(self)[field] end, __newindex = function(self, name, value) if name~=field then error("attempt to index a nil value") end getmetatable(self)[field] = value end} ) _G:method_missing(function(self, name, ...) local args = {...} if name=="test_print" then print("Oh my lord, it method missing!", ...) return elseif args[1] and string.find(name, args[1]) then --If the first argument is in the name called... table.remove(args, 1) return unpack(args) end end) test_print("I like me some method_missing abuse!") test_print("Do it again!") print(test_print, "method_missing magic!") print(this_should_be_nil == nil, this_should_be_nil() == nil) print(conditional_runs("runs", "conditionally", "due to args")) print(conditional_runs("While this does nothing!")) --Apparently this doesn't print 'nil'... why?
Output:
Oh my lord, it method missing! I like me some method_missing abuse! Oh my lord, it method missing! Do it again! nil method_missing magic! true true conditionally due to args
This snippet allows you to use method_missing quite similar to how you can work in Ruby (but don't check the answer at the same time). This is similar to my original answer, except that it “passes the dollar” through nil metatable, which I thought I couldn’t do. (thanks for the tip!) But, as @greatwolf says, there is probably no reason to use a construct like this in Lua; the same dynamism can probably be achieved through clearer manipulations with metamethods.