Class: Rufus::Lua::Table
- Includes:
- Enumerable
- Defined in:
- lib/rufus/lua/objects.rb
Overview
A Lua table.
For now, the only thing you can do with it is cast it into a Hash or an Array (will raise an exception if casting to an Array is not possible).
Note that direct manipulation of the Lua table (inside Lua) is not possible (as of now).
Constant Summary
Constants included from StateMixin
StateMixin::LUA_ENVIRONINDEX, StateMixin::LUA_GLOBALSINDEX, StateMixin::LUA_MULTRET, StateMixin::LUA_NOREF, StateMixin::LUA_REFNIL, StateMixin::LUA_REGISTRYINDEX, StateMixin::TBOOLEAN, StateMixin::TFUNCTION, StateMixin::TLIGHTUSERDATA, StateMixin::TNIL, StateMixin::TNONE, StateMixin::TNUMBER, StateMixin::TSTRING, StateMixin::TTABLE, StateMixin::TTHREAD, StateMixin::TUSERDATA
Instance Attribute Summary
Attributes inherited from Ref
Instance Method Summary collapse
-
#[](k) ⇒ Object
Returns the value behind the key, or else nil.
-
#each ⇒ Object
The classical ‘each’.
-
#keys ⇒ Object
Returns the array of keys of this Table.
-
#to_a ⇒ Object
Returns a Ruby Array instance representing this Lua table.
-
#to_h ⇒ Object
Returns a Ruby Hash instance representing this Lua table.
-
#values ⇒ Object
Returns the array of values in this Table.
Methods inherited from Ref
Constructor Details
This class inherits a constructor from Rufus::Lua::Ref
Instance Method Details
#[](k) ⇒ Object
Returns the value behind the key, or else nil.
185 186 187 188 189 190 191 |
# File 'lib/rufus/lua/objects.rb', line 185 def [] (k) load_onto_stack # table stack_push(k) # key Lib.lua_gettable(@pointer, -2) # fetch val for key at top and table at -2 stack_pop end |
#each ⇒ Object
The classical ‘each’.
Note it cheats by first turning the table into a Ruby Hash and calling the each of that Hash instance (this way, the stack isn’t involved in the iteration).
160 161 162 163 164 |
# File 'lib/rufus/lua/objects.rb', line 160 def each return unless block_given? self.to_h.each { |k, v| yield(k, v) } end |
#keys ⇒ Object
Returns the array of keys of this Table.
169 170 171 172 |
# File 'lib/rufus/lua/objects.rb', line 169 def keys self.to_h.keys end |
#to_a ⇒ Object
Returns a Ruby Array instance representing this Lua table.
Will raise an error if the ‘rendering’ is not possible.
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/rufus/lua/objects.rb', line 232 def to_a h = self.to_h keys = h.keys.sort keys.find { |k| not [ Float ].include?(k.class) } && raise("cannot turn hash into array, some keys are not numbers") keys.inject([]) { |a, k| a << h[k]; a } end |
#to_h ⇒ Object
Returns a Ruby Hash instance representing this Lua table.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/rufus/lua/objects.rb', line 204 def to_h load_onto_stack table_pos = stack_top Lib.lua_pushnil(@pointer) h = {} while Lib.lua_next(@pointer, table_pos) != 0 do value = stack_fetch(-1) key = stack_fetch(-2) stack_unstack # leave key on top h[key] = value end h end |
#values ⇒ Object
Returns the array of values in this Table.
177 178 179 180 |
# File 'lib/rufus/lua/objects.rb', line 177 def values self.to_h.values end |