如果你的系統已經安裝了 Rudix,只需要執行 sudo rudix install lua 即可,否則 Rudix 提供一些獨立的軟件包用來安裝,打開你的瀏覽器并根據系統選擇相應的版本來安裝。
Lua 將默認安裝到 /usr/local/ ,Lua 解釋器完整的路徑是 /usr/local/bin/lua,但你不需要輸入完整的路徑來調用 Lua,在終端中運行 lua 即可調用解釋器。
在 Mountain Lion 下事情稍微有一點點麻煩,你在運行安裝程序時可能會提示這樣的錯誤信息:“lua-5.2.1-0.pkg” can't be opened because it is from an unidentified developer. 這表示這個軟件包沒有在蘋果上注冊過,但你可以選擇信任 Rudix 開發者,然后進行安裝。
-- Fibonacci sequence with coroutines
function fibo()
a, b = 0, 1
while true do
coroutine.yield(a)
a, b = b, a + b
end
end
co = coroutine.create(fibo)
n = arg[1] or 20
for i = 0, n do
print(i,coroutine.resume(co))
end