Date post: | 02-Jul-2015 |
Category: |
Documents |
Upload: | prayoch-rujira |
View: | 155 times |
Download: | 5 times |
Prayoch Rujira@Clockup studio
install Homebrew
http://brew.sh/
install Lua
$>brew install lua
INSTALLATION
print("Hello world")
Create file hello.lua and write this code
And then execute in command line$>lua hello.lua
THE FIRST PROGRAM
NO TYPE DECLARATION
somchai = "somchai" print(somchai) somchai = 1984 print(somchai) somchai = {} print(somchai)
GLOBAL BY DEFAULT function createSomchai() somchai = "My father" end !
function greetSomchai() print("Hello"..somchai) end !
createSomchai() print(somchai) greetSomchai()
LOCAL DECLARATION
functon createSomchai() local somchai = "My father" end
TABLE RULES!
There is No Array, Class, Dictionary etc. Just Table
TABLE AS ARRAY
somchaiChildrens = { "Hercules", "Perseus", "Kratos" }
TABLE AS DATA OBJECT
somchai = { name = "somchai", age=42, hasMarried=true }
TABLE AS A MODULE
somchai = { function sleep() end function wake() end function work() end }
ALSO OTHERS DATA STRUCTURE
Queue, Set, LinkedList, Tree and more. Are apply on table
FUNCTIONfunction subProcess() print("I'm subprocess") end !
function mainProcess(sub) sub() end !
mainProcess(subProcess)
FUNCTIONrules = { function() print("I'm a") end, function() print("I'm b") end, } !for key, value in pairs(rules) do value() end
NO TRY-CATCH !?When dealing with function that will throw error use
pcall instead.
function willThrowError() error("That's error!!") end !status, result = pcall(willThrowError) !print("status="..tostring(status)) print("errorMessage="..result)
NO TRY-CATCH !?function willNotThrowError() return "No error" end !
status, result = pcall(willNotThrowError) !
print("status="..tostring(status)) print("realResult="..result)
WRITING A MODULE
local M = {} !
function M.doSomething() end !
return M
IMPORT AND USE
m = require("M") !
m.doSomething()
UNIT TESTING FRAMEWORK
LuaUnit https://github.com/bluebird75/luaunit !
Busted http://olivinelabs.com/busted/
INSTALL BUSTED
Install Luarocks $>brew install luarocks
!
Install Busted $>luarocks install busted
!
Then try to execute busted $>busted
THE FIRST TESTCreate directory ‘spec’ and create file ‘first_spec.lua’
into it.
describe("The first test", function() it("false should not be true", function() assert.True(false) end) end)
THE FIRST TESTExecute ‘busted’ at root of project directory
$>busted
OK Bye…