about processes and engines

Archive for the ‘lua’ Category

rufus-lua 1.1.0

luaJust released rufus-lua 1.1.0. The original post I wrote about rufus-lua is named ruby to lua.

This release strongly benefited from Scott Persinger’s work on Laminate, a tool for safe user-authored templates for Vodpod.

Scott needed strong support for callbacks from Lua to Ruby and also ways to pass values back and forth. Many thanks to Scott for the great collaboration.

Those of you interested in Lua and web applications should have a look at Norman Clarke’s lua-haml and at Daniele Alessandri’s mercury (sinatra-like web framework for Lua).


Written by John Mettraux

September 30, 2009 at 5:04 am

Posted in lua, ruby, rufus

edo cabinet – rubykaigi2009

Here are my slides for the “edo cabinet” talk at the RubyKaigi2009.

I presented about Ruby-FFI, Tokyo Cabinet|Tyrant and then rufus-tokyo and Rufus::Edo.

These are just slides, here is the english transcript of the actual talk.


Many thanks to the RubyKaigi team for organizing this great event ! Special kudos to Leonard Chin for the on-the-fly translations, throughout the 3 days.

You’ll find videos of most of the talks on (thanks to the KaigiFreaks) and most of the slides are available from Slideshare.


Written by John Mettraux

July 28, 2009 at 11:57 pm

rufus-tokyo 0.1.12, ext(lua)

tokyo-luaJust released version 0.1.12 of rufus-tokyo, a Ruby library for accessing Tokyo Cabinet and Tokyo Tyrant (via FFI or via the Ruby classes provided with the Tokyo products).

Tokyo Tyrant, once successfully compiled with –enable-lua, is open to lots of interesting usages.

In his last post on the Mixi dev blog, the author of Tokyo Cabinet / Tyrant is exposing his ideas about an application of map/reduce leveraging Tokyo Tyrant (post in Japanese, machine translation here). This demonstration relies on the embedded Lua runtime for the mapper/reducer implementation.

For this post, I’ll just show the vanilla “incr” function found in the Tokyo Tyrant documentation :

-- incr.lua
function incr (key, i)
  i = tonumber(i)
  if not i then
    return nil
  local old = tonumber(_get(key))
  if old then
    i = old + i
  if not _put(key, i) then
    return nil
  return i

This Lua function increments the current value bound for a key by a given amount.

You start your Lua-enabled Tokyo Tyrant with something like :

  ttserver -ext incr.lua test.tch

and then hit it with code like :

# test.rb

require 'rubygems'
require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo

t ='', 1978)

5.times do
  puts t.ext(:incr, 'my-counter', 2).to_i


The “ext” method is available with the latest release of rufus-tokyo, for the Rufus::Tokyo::Tyrant and Rufus::Edo::NetTyrant classes.

I updated my documentation about Tokyo Cabinet / Tyrant install with some –enable-lua information.

This release of rufus-tokyo also adds transaction support for the Rufus::Tokyo::Cabinet class (requires Tokyo Cabinet 1.4.13).


Written by John Mettraux

April 7, 2009 at 5:32 am

rufus-lua 1.0 out

luaJust released rufus-lua 1.0

There are two main new features : lua garbage collection control (thanks Alain) and the possibility to bind Ruby blocks as Lua functions.

Rufus-lua is available via

sudo gem install rufus-lua


Written by John Mettraux

March 27, 2009 at 6:26 am

Posted in lua, ruby, rufus

ruby to lua

luaWith the help from Alain I just released rufus-lua 0.1.0.

This ruby gem talks to Lua via Ruby FFI. One could say it’s “embedding Lua”, as Lua is described as “Lua is a powerful, fast, lightweight, embeddable scripting language.”

I feel tempted by Lua, there’s the speed, the austerity and that javascript feeling.


Rufus-lua is not the first bridge between Lua and Ruby, there are also :


They are worth a look, but I was a bit disappointed, they are not available via ‘sudo gem install …’.

Rufus-lua is available via Rubygems, but since it’s a FFI thing, it needs to talk to the dynamic library packaging of Lua. Well, it’s not available by default, there’s some work involved (but not too much).

Something great about FFI is that it works for Ruby 1.8.6, Ruby 1.9.1 and JRuby (1.1.6) (and will work soon for Rubinius, its original platform).


Once liblua.dylib is on your system, along with rufus-lua (sudo gem install rufus-lua), you can play with code like :

require 'rubygems'
require 'rufus/lua'

s =

t = s.eval(%{
  return { message = { 'hello', 'from', 'Lua' },
    funk = function (x) return 2^x end }

p t['message'].to_a # => ["hello", "from", "lua"]
p t['funk'].call(8) # => 256.0


Well, it’s only a 0.1.0 release…


As rufus-lua is using FFI it’s slower at the “frontier”, here is a naive benchmark for computing a Fibonacci suite, with Fiber (Ruby 1.9) and Coroutines on the Lua side :

require 'benchmark'
require 'rubygems'
require 'rufus/lua'

  n1 = n2 = 1
  loop do
    Fiber.yield n1
    n1, n2 = n2, n1+n2

s = %{
  co = coroutine.create(function ()
    local n1 = 1; local n2 = 1
    while true do
      n1, n2 = n2, n1+n2
  return co

LUAFIBS = LUA.eval(s)

N = 10_000
Benchmark.benchmark(' ' * 31 + Benchmark::Tms::CAPTION, 31) do |b|'ruby') do
    N.times { RUBYFIBS.resume }
  end'lua via ruby') do
    N.times { LUAFIBS.resume }
  end'lua') do
    LUA.eval("for i = 0, #{N} do coroutine.resume(co) end")


For the original. Running it on my board yields something along the lines of :

$ ruby19 test/bm_fiber.rb
                  user     system      total        real
ruby          0.050000   0.010000   0.060000 (  0.052032)
lua via ruby  0.180000   0.000000   0.180000 (  0.195411)
lua           0.010000   0.000000   0.010000 (  0.006394)

Well, nothing spectacular, that just states that it’s better to not cross the border too often : the “lua via ruby” bench has Ruby resuming the Lua coroutine 10_000 times and it’s costly.


on github :
rdoc :
the rest of rufus :

lua :
ffi :


Written by John Mettraux

March 16, 2009 at 9:39 am

Posted in ffi, jruby, lua, ruby, rufus