Gem Version

Lua embedded in Ruby, via Ruby FFI.

(Lua 5.1.x only, sorry).

Lua says :

Lua is a powerful, fast, lightweight, embeddable scripting language.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

other Ruby and Lua bridges / connectors

getting Lua on your system

On Debian GNU/Linux, I do

sudo apt-get install liblua5.1-0

If your system's package manager doesn't have some version (5.1.x) of Lua around, jump to compiling liblua.dylib below.

Rufus-lua will look for library in a list of know places.

If it doesn't find the Lua dynamic library or if it picks the wrong one, it's OK to set the LUA_LIB environment variable. For example:

LUA_LIB=~/mystuff/ ruby myluacode.rb


export LUA_LIB=~/mystuff/
# ...
ruby myluacode.rb

using rufus-lua

gem install rufus-lua

or add to your Gemfile:

  gem 'rufus-lua'


require 'rufus-lua'

s =

puts s.eval("return table.concat({ 'hello', 'from', 'Lua' }, ' ')")
  # => "Hello from Lua"


binding Ruby code as Lua functions

require 'rufus-lua'

s =

s.function 'key_up' do |table|
  table.inject({}) do |h, (k, v)|
    h[k.to_s.upcase] = v

p s.eval(%{
  local table = { CoW = 2, pigs = 3, DUCKS = 'none' }
  return key_up(table) -- calling Ruby from Lua...
  # => { 'COW' => 2.0, 'DUCKS => 'none', 'PIGS' => 3.0 }


It's OK to bind a function inside of a table (library):

require 'rufus-lua'

s =

s.eval("rubies = {}")
s.function 'add' do |x, y|
  x + y

s.eval("rubies.add(1, 2)")
  # => 3.0


You can omit the table definition (only 1 level allowed here though):

require 'rufus-lua'

s =

s.function 'rubies.add' do |x, y|
  x + y

s.eval("rubies.add(1, 2)")
  # => 3.0


The specs contain more examples:

eval(code[, binding[, filename[, lineno ]]])

The examples so far have shown Rufus::Lua::State#eval being used with a single argument, a piece of code.

But this rufus-lua eval mimics the Ruby eval and lets one specify binding, filename and lineno.

(TODO) Binding hasn't yet been implemented. It'll probaby be with setfenv but nothing sure yet. Stick a nil to it for now.

The string of Lua code may come from wild places, it may help to flag it with arbitrary filename and lineno.

require 'rufus-lua'

lua =

lua.eval('print("hello")', nil, 'myluastuff/hello.lua', 77)

compiling liblua.dylib

original instructions by Adrian Perez at:

get the source at:


tar xzvf lua-5.1.4.tar.gz
cd lua-5.1.4

Modify the file src/Makefile as per

It's mostly about adding that rule to the src/Makefile:

liblua.dylib: $(CORE_O) $(LIB_O)
    $(CC) -dynamiclib -o [email protected] $^ $(LIBS)

Here's how to build the library file and deploy it:

make macosx # or make linux ...
make -C src liblua.dylib
sudo cp src/liblua.dylib /usr/local/lib/

sudo make macosx install

I tend to copy the lib with

sudo cp src/liblua.dylib /usr/local/lib/liblua.5.1.4.dylib

# instead of
#sudo cp src/liblua.dylib /usr/local/lib/

tested with

ruby 1.8.7p72, ruby 1.9.1p0, jruby 1.2.0 jruby 1.1.6 has an issue with errors raised inside of Ruby functions (callbacks)

ruby-ffi 0.4.0 and 0.5.0

I run the specs with

bundle install # first time only
bundle exec rspec


the ruby gem 'ffi'

issue tracker


git clone git://

authors and credits

see CREDITS.txt



Lua itself is licensed under the MIT license as well :