0.0
No commit activity in last 3 years
No release in over 3 years
A wrapper to pass complex objects between Ruby and Lua
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 10.4
~> 3.3

Runtime

~> 1.0
 Project Readme

Description

This library makes it easy to provide a complex API for Lua scripts. It uses RLua internally and therefore it has the same compatibility and restrictions.

Example code

Lets say we have these classes:

User = Struct.new(:name, :favorite_food, :age)
Food = Struct.new(:name, :cooking_time) do
  def prepare(timer)
    if timer == cooking_time
      "This #{name} is delicious"
    elsif timer > cooking_time
      "This #{name} is awfully burnt"
    else
      "Smells like frozen #{name}..."
    end
  end

  def buy(*)
    'Got one'
  end
end

Pizza   = Food.new('Pizza', 16)
Lasagne = Food.new('Lasagne', 40)

class Sandbox
  def get(username)
    users.detect { |u| u.name == username }
  end

  def users
    [
        User.new('Kyle', Pizza, 43),
        User.new('Lisa', Pizza, 25),
        User.new('Ben', Lasagne, 6),
    ]
  end
end

To make it available to our Lua script we can use this code:

require 'transparent_lua'

tlua = TransparentLua.new(Sandbox.new)
tlua.call(<<-EOF)
      print(get_user('Kyle').name .. " likes " .. get_user('Kyle').favorite_food.name .. ".");
      print(get_user('Kyle').favorite_food.buy());
      print("It needs to cook exactly " .. get_user('Kyle').favorite_food.cooking_time .. " minutes");

      my_cooking_time = 270;
      print(get_user('Kyle').name .. " cooks it for " .. my_cooking_time .. " minutes.");
      print(get_user('Kyle').favorite_food.prepare(my_cooking_time));
EOF

Pizza.cooking_time = 270

tlua.call(<<-EOF)
      print("Lets try it again for " .. my_cooking_time .. " minutes. Maybe it works now...");

      print(get_user('Kyle').name .. " cooks it for " .. my_cooking_time .. " minutes.");
      print(get_user('Kyle').favorite_food.prepare(my_cooking_time));
EOF

Types of methods

  • Methods without arguments are created as index of the Lua table
  • Methods with arguments are created as a callable metatable.
  • To make clear, that an argumentless method is a method (food.buy() vs. food.buy), discard any arguments (def buy(*))

Type conversion

In addition to RLuas type conversion, this library converts Lua tables to either Hashes or Arrays (when all keys are Numeric).