Lua Missions
下載:
https://github.com/kikito/lua_missions
missions目錄爲題目,src目錄爲答案。
如何運行:
cd lua_missions\missions
lua missions.lua
學習的形式
通過填空的形式,學習lua的語法和API。例如:
function test_string_find_a_pattern()
local str = 'banana'
local pattern = 'an'
local start_pos, end_pos = string.find(str, pattern)
assert_equal(__(2), start_pos)
assert_equal(__(3), end_pos)
end
第一次運行lua missions.lua,查看當前完成進度,會顯示:
*** Mission status ***
asserts...........................................[Incomplete]
test_assert: [fail]
Assertion failed: Expected [false] to be [true]
The error happened here:
asserts.lua:3: in function <asserts.lua:2>
這裏的error不要緊,正是我們需要填的空,所有的error都改完了就算合格!!
共有以下專題:
'asserts',
'variables',
'numbers',
'strings',
'patterns',
'control',
'truthiness',
'functions',
'errors',
'load',
'tables',
'tables_and_functions',
'meta',
'indices',
'exercise',
'weak_tables'
每個專題一個文件,按照missions.lua的引導,注意用文本編輯器修改error完成學習,關鍵不在改錯,請關注每一個例子的名字,名字爲這個例子要學習的目的!!
基於lua5.1,有些語法在lua5.3中不適用,例如loadstring 被load代替,unpack被tableunpack代替等,需要注意,否則總是改不對。
其他學習資源:
Programming In Lua(官方文檔)
Beginning Lua Programming(比官方文檔更細緻,有點厚)
http://lua-users.org/wiki/LearningLua
祝大家學習快樂!!