Key-value data store
require "redis"
redis = Redis.new('localhost', 6379)
redis.set("name", "peter")
name = redis.get("name") # "peter"
redis.set("age", "25")
new_age = redis.incr("age") # 26
Concurrent, but single threaded
counter = redis.get('counter')
redis.incr('counter') if counter.is_a? Numeric
script = <<-EOS
local counter = redis.call("GET", KEYS[1])
if type(tonumber(counter)) == "number" then
return redis.call("INCR", KEYS[1])
end
EOS
redis.eval(script, ["counter"])
brew install vim --with-lua
-or-
brew install macvim --with-lua
Startup Speed
Vim script 0 1.498s
Python 0.0166s 0.027s (2000%)
Lua(jit) 0.0002s 0.001s (1152000%)
coronalabs.com/i-want-to-build/games/
coronalabs.com/i-want-to-build/business-apps/
A general-purpose language to make programmers happy
Portable, powerful, embeddable, fast.
# ruby
name = 'Peter'
age = 35
genter = :male
puts "#{name} is a #{age} years old #{gender}"
doc = %{
Lorem ipsum dolor sit amet
}
-- lua
local name = 'Peter'
local age = 35
local genter = "male"
print(name .. " is a " .. age .. ' years old ' .. gender)
local doc = [[
Lorem ipsum dolor sit amet
]]
Ruby comments:
# a single-line comment in ruby
=begin
a multi-line comment in ruby
=end
Lua comments:
-- a single-line comment in Lua
--[[
a multi-line comment in Lua
]]
#ruby
h = {a: 1, b: 2}
puts h[:b] # 2
puts a.class.name # 'Hash'
a = [1,2,3,4]
puts a[0] # 1
puts a.class.name # 'Array'
-- lua
local h = {a = 1, b = 2}
print(h['b']) -- 2
print(h.b) -- also 2
print(type(b)) -- 'table'
local a = {1,2,3,4}
print(a[1]) -- 1
print(type(a)) -- 'table'
#ruby
def sum(a,b)
a + b
end
#ruby
module Kernel
private # ??
def sum(a,b)
a + b
end
end
-- lua
function sum(a+b)
return a + b
end
print(type(sum)) -- 'function'
print(type(type)) -- 'function'
print(type(print)) -- 'function'
-- lua
_G.sum = function(a+b)
return a + b
end
print(type(_G)) -- 'table'
-- lua
local sum = function(a+b)
return a + b
end
even_numbers = (1..10).select(&:even?)
tweets.delete_if{ |t| t.older_than? 1.week }
// Very little of this stuff:
[9,10,1,2].sort()
> [1,10,2,9]
“Learn Lua in 15 minutes” http://tylerneylon.com/a/learn-lua/
git clone https://github.com/ruby/ruby.git
rm -rf ruby/benchmark ruby/bootstraptest ruby/doc \
ruby/ext ruby/sample ruby/test ruby/tool
cloc ruby
git clone https://github.com/LuaDist/lua.git
cloc lua/src
$ irb
> a = Object.constants.map{|c| Object.const_get(c)}
=> [ ... ]
> b = a.select{|c| c.is_a? Class}
=> [ Object, Module, Class, BasicObject, Method,
NilClass, String, Symbol, Regexp, Range,
Time, Date, File, Dir, TrueClass, FalseClass,
Numeric, Integer, Fixnum, Float, Bignum,
Rational, Complex, Thread, Fiber, ...
] (73)
> b.map{|c| c.methods.count}.inject(:+)
=> 7112
> b.map{|c| c.public_methods(false).count}.inject(:+)
=> 399
set yaml minitest erb
base64 csv mutex webrick
date json zlib net/*
fileutils matrix curses socket
pp profile rss uri
...
(~109)
Presentation: “Ruby: Batteries Included”:
Types (~7)
string, number, table, boolean, function, thread, nil
Libraries (~7)
string, math, table, os, io, coroutine, debug
Top-level functions (~30)
assert load pcall setmetatable
collectgarbage loadfile print tonumber
dofile loadstring rawequal tostring
error module rawget type
gcinfo newproxy rawset xpcall
getfenv next unpack
getmetatable require select
ipairs pairs setfenv
Artillery battery: a unit of guns, mortars, rockets or missiles so grouped in order to facilitate better battlefield communication and command and control