Skip to content

Instantly share code, notes, and snippets.

@LPGhatguy
Created June 19, 2013 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LPGhatguy/5810806 to your computer and use it in GitHub Desktop.
Save LPGhatguy/5810806 to your computer and use it in GitHub Desktop.
This is a fun thing that allows select Ruby code (namely loops with ranges) to execute as valid Lua code.
--Luby 1.0
function construct_iterator(n)
local at = 0
return function()
at = at + 1
return n[at]
end, n
end
local p_meta = {
__concat = function(first, second)
local out = {}
for index = first[1], second[1] do
out[#out + 1] = index
end
return construct_iterator(out)
end,
__add = function(first, second)
return ((type(first) == "table") and first[1] or first) + ((type(second) == "table") and second[1] or second)
end,
__sub = function(first, second)
return ((type(first) == "table") and first[1] or first) - ((type(second) == "table") and second[1] or second)
end,
__div = function(first, second)
return ((type(first) == "table") and first[1] or first) / ((type(second) == "table") and second[1] or second)
end,
__mult = function(first, second)
return ((type(first) == "table") and first[1] or first) * ((type(second) == "table") and second[1] or second)
end,
}
setmetatable(getfenv(), {
__newindex = function(self, key, value)
if (type(value) == "number") then
rawset(getfenv(), "out", {value})
setmetatable(out, p_meta)
end
rawset(self, key, out or value)
end
})
return {}
print(#require("luby")
)
a = 1
b = 10
for i in (a..b) do
print(i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment