+ All Categories
Home > Documents > Introduction to lua

Introduction to lua

Date post: 02-Jul-2015
Category:
Upload: prayoch-rujira
View: 155 times
Download: 5 times
Share this document with a friend
26
Prayoch Rujira@Clockup studio
Transcript
Page 1: Introduction to lua

Prayoch Rujira@Clockup studio

Page 2: Introduction to lua

http://www.lua.org/

LANGUAGE FROM THE MOON

Page 3: Introduction to lua

install Homebrew

http://brew.sh/

install Lua

$>brew install lua

INSTALLATION

Page 4: Introduction to lua

http://www.thijsschreijer.nl/blog/?p=863

FOR WINDOWS

Page 5: Introduction to lua

print("Hello world")

Create file hello.lua and write this code

And then execute in command line$>lua hello.lua

THE FIRST PROGRAM

Page 6: Introduction to lua

NO TYPE DECLARATION

somchai = "somchai" print(somchai) somchai = 1984 print(somchai) somchai = {} print(somchai)

Page 7: Introduction to lua

GLOBAL BY DEFAULT function createSomchai() somchai = "My father" end !

function greetSomchai() print("Hello"..somchai) end !

createSomchai() print(somchai) greetSomchai()

Page 8: Introduction to lua

LOCAL DECLARATION

functon createSomchai() local somchai = "My father" end

Page 9: Introduction to lua

TABLE RULES!

There is No Array, Class, Dictionary etc. Just Table

Page 10: Introduction to lua

TABLE AS ARRAY

somchaiChildrens = { "Hercules", "Perseus", "Kratos" }

Page 11: Introduction to lua

TABLE AS DATA OBJECT

somchai = { name = "somchai", age=42, hasMarried=true }

Page 12: Introduction to lua

TABLE AS A MODULE

somchai = { function sleep() end function wake() end function work() end }

Page 13: Introduction to lua

ALSO OTHERS DATA STRUCTURE

Queue, Set, LinkedList, Tree and more. Are apply on table

Page 14: Introduction to lua

FUNCTIONfunction subProcess() print("I'm subprocess") end !

function mainProcess(sub) sub() end !

mainProcess(subProcess)

Page 15: Introduction to lua

FUNCTIONrules = { function() print("I'm a") end, function() print("I'm b") end, } !for key, value in pairs(rules) do value() end

Page 16: Introduction to lua

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)

Page 17: Introduction to lua

NO TRY-CATCH !?function willNotThrowError() return "No error" end !

status, result = pcall(willNotThrowError) !

print("status="..tostring(status)) print("realResult="..result)

Page 18: Introduction to lua

WRITING A MODULE

local M = {} !

function M.doSomething() end !

return M

Page 19: Introduction to lua

IMPORT AND USE

m = require("M") !

m.doSomething()

Page 20: Introduction to lua

UNIT TESTING FRAMEWORK

LuaUnit https://github.com/bluebird75/luaunit !

Busted http://olivinelabs.com/busted/

Page 21: Introduction to lua

INSTALL BUSTED

Install Luarocks $>brew install luarocks

!

Install Busted $>luarocks install busted

!

Then try to execute busted $>busted

Page 22: Introduction to lua

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)

Page 23: Introduction to lua

THE FIRST TESTExecute ‘busted’ at root of project directory

$>busted

Page 24: Introduction to lua

SAMPLE CODE IN SLIDE

https://github.com/j4cksw/lua-samples

Page 25: Introduction to lua
Page 26: Introduction to lua

OK Bye…


Recommended