Module: Rufus::Lua

Defined in:
lib/rufus/lua/lib.rb,
lib/rufus/lua/error.rb,
lib/rufus/lua/state.rb,
lib/rufus/lua/utils.rb,
lib/rufus/lua/objects.rb,
lib/rufus/lua/version.rb

Defined Under Namespace

Modules: Lib, StateMixin Classes: CallbackState, Coroutine, Function, LuaError, Ref, State, Table

Constant Summary collapse

LUA_ERRS =

error codes from 0 to 5

%w[ 0 LUA_YIELD LUA_ERRRUN LUA_ERRSYNTAX LUA_ERRMEM LUA_ERRERR ]
VERSION =
'1.1.5'

Class Method Summary collapse

Class Method Details

.to_lua_s(o) ⇒ Object

Turns a Ruby instance into a Lua parseable string representation.

Will raise an ArgumentError as soon as something else than a simple Ruby type (or Hash/Array) is passed.

Rufus::Lua.to_lua_s({ 'a' => 'A', 'b' => 2})
  #
  # => '{ "a": "A", "b": 2 }'


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rufus/lua/utils.rb', line 18

def self.to_lua_s(o)

  case o

  when String then o.inspect
  when Integer then o.to_s
  when Float then o.to_s
  when TrueClass then o.to_s
  when FalseClass then o.to_s
  when NilClass then 'nil'

  when Hash then to_lua_table_s(o)
  when Array then to_lua_table_s(o)

  else fail(
    ArgumentError.new(
      "don't how to turning into a Lua string representation "+
      "Ruby instances of class '#{o.class}'"))
  end
end

.to_lua_table_s(o) ⇒ Object

Turns a Ruby Array or Hash instance into a Lua parseable string representation.



42
43
44
45
46
47
48
49
50
51
# File 'lib/rufus/lua/utils.rb', line 42

def self.to_lua_table_s(o)

  s = if o.is_a?(Array)
    o.collect { |e| to_lua_s(e) }
  else
    o.collect { |k, v| "[#{to_lua_s(k)}] = #{to_lua_s(v)}" }
  end

  "{ #{s.join(', ')} }"
end