Making Lua Like Ruby


Let's give Lua some of the semantics of Ruby [1]. For example:

;(3):times(function(x) print(x) end)
hash = H{x = 1, y = 2, z = 3}
hash:delete_if(function(key, value) return key == 'y' end)
hash:each(function(key, value)
 puts(key, value)
end)
--[[OUTPUT:
1
2
3
x 1
z 3
--]]

which is implemented as such:

-- This is rather incomplete but is a start.
-- ruby numbers
local mt = {}
debug.setmetatable(0, mt)
local funcs = {}
function funcs.times(num, func)
 for i=1,num do func(i) end
end
mt.__index = funcs;
-- ruby hash
local h_mt = {}
h_mt.__index = h_mt
function h_mt:each(func)
 for k,v in pairs(self) do func(k,v) end
end
function h_mt:delete_if(func)
 for k,v in pairs(self) do
 if func(k,v) then self[k] = nil end
 end
end
function H(t)
 return setmetatable(t, h_mt)
end
-- ruby functions
puts = print

--DavidManura

See Also

Backlinks such as the following are clutter-- page title link already provides them

In a way, yes, but it involves two clicks rather than one to navigate to them and is not very visible. It may be better if backlinks were displayed on the bottom of a page. --DavidManura


RecentChanges · preferences
edit · history
Last edited May 1, 2009 8:24 pm GMT (diff)

AltStyle によって変換されたページ (->オリジナル) /