Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

Lua for Rubyists

Enrique García Cota (@otikik)

madrid-rb, 2014-01

2 main parts:

Applications

Comparison

It's Lua, not LUA

fullscreen

SOL ⇒

Simple Object Language

fullscreen

Lua ⇒

“Moon” in Portuguese

(No Initials)

fullscreen

fullscreen

fullscreen

Part 1: Applications

fullscreen

fullscreen

fullscreen

fullscreen

www.techempower.com/benchmarks

centered

www.techempower.com/benchmarks

centered

fullscreen

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

fullscreen


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"])

fullscreen

centered

brew install vim --with-lua

-or-

brew install macvim --with-lua

fullscreen

fullscreen

                    Startup     Speed

      Vim script    0           1.498s
      Python        0.0166s     0.027s (2000%)
      Lua(jit)      0.0002s     0.001s (1152000%)

Corona SDK

centered

coronalabs.com/i-want-to-build/games/

fullscreen

centered

coronalabs.com/i-want-to-build/business-apps/

centered

www.love2d.org

centered

Part 2: Comparison

Generalities

float-left

Yukihiro Matsumoto

A general-purpose language to make programmers happy

fullscreen

fullscreen

float-left

Roberto Ierusalimschy

Portable, powerful, embeddable, fast.

fullscreen

fullscreen

Implementations

Ruby

MRI (1.8.7, 1.9.x, 2.x)

JRuby

Rubinius

fullscreen

fullscreen

Lua

Lua (5.0, 5.1, 5.2)

LuaJIT

eLua

fullscreen

fullscreen

Feel

# 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'

Lua tables:

#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

Lua functions:

Ruby feels ♡ ♡ ♡


even_numbers = (1..10).select(&:even?)

tweets.delete_if{ |t| t.older_than? 1.week }

Lua feels like super-javascript


// 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/

Size

Lines of code

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

⇒ ~650k (C, C++, Ruby)

git clone https://github.com/LuaDist/lua.git
cloc lua/src

⇒ ~15k (C99)

Ruby core:

$ 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

Ruby Stdlib:

www.ruby-doc.org/stdlib-2.1.0


     set         yaml       minitest     erb
     base64      csv        mutex        webrick
     date        json       zlib         net/*
     fileutils   matrix     curses       socket
     pp          profile    rss          uri
                        ...

                       (~109)

float-left

Ruby ⇒ gems

rubygems.org

~70000 gems

Lua:

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

float-left

Lua ⇒ rocks

luarocks.org

~700 rocks

Batteries included

fullscreen

Artillery battery: a unit of guns, mortars, rockets or missiles so grouped in order to facilitate better battlefield communication and command and control

fullscreen

fullscreen

fullscreen

fullscreen

fullscreen

Conclusion

Ruby is great.

Lua is great.

fullscreen

Questions?

Enrique García Cota (@otikik)

madrid-rb

Thank you!