Module: Rufus::Lua

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'1.1.0'

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


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rufus/lua/utils.rb', line 42

def self.to_lua_s (o)

  case o

    when String then o.inspect
    when Fixnum then o.to_s
    when Float then o.to_s
    when TrueClass then o.to_s
    when FalseClass then o.to_s

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

    else raise(
      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.



65
66
67
68
69
70
71
72
73
74
# File 'lib/rufus/lua/utils.rb', line 65

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