Class: Rufus::Lua::Table
- Rufus::Lua::Ref
- Rufus::Lua::Table
Included Modules
Enumerable
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).
Constructor Summary
This class inherits a constructor from Rufus::Lua::Ref.
Public Visibility
Public Instance Method Summary
| #[](k) |
Returns the value behind the key, or else nil. |
|---|---|
| #[]=(k, v) |
Sets a value in the table. |
| #each |
The classical ‘each’. |
| #keys |
Returns the array of keys of this Table. |
| #objlen |
Returns the size of the table, corresponds to the Lua ’#’ operator. |
| #size #length |
Returns the real size of the table (number of entries + number of elements in array side). |
| #to_a(pure = true) |
Returns a Ruby Array instance representing this Lua table. |
| #to_h |
Returns a Ruby Hash instance representing this Lua table. |
| #to_ruby |
Turns the Lua table into a Ruby array, or else into a Ruby Hash instance. |
| #values |
Returns the array of values in this Table. |
Public Instance Methods Inherited from Rufus::Lua::Ref
Public Instance Method Details
[]
Returns the value behind the key, or else nil.
178 179 180 181 182 183 184 |
# File 'lib/rufus/lua/objects.rb', line 178 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 |
[]=
Sets a value in the table
TODO : have something for adding in the array part…
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rufus/lua/objects.rb', line 190 def []= (k, v) load_onto_stack stack_push(k) stack_push(v) Lib.lua_settable(@pointer, -3) v end |
each
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).
156 157 158 159 160 |
# File 'lib/rufus/lua/objects.rb', line 156 def each return unless block_given? self.to_h.each { |k, v| yield(k, v) } end |
keys
Returns the array of keys of this Table.
164 165 166 167 |
# File 'lib/rufus/lua/objects.rb', line 164 def keys self.to_h.keys end |
objlen
Returns the size of the table, corresponds to the Lua ’#’ operator.
Will thus return 0 if the table doesn’t hold any value in its ‘array’ part.
206 207 208 209 210 |
# File 'lib/rufus/lua/objects.rb', line 206 def objlen load_onto_stack Lib.lua_objlen(@pointer, -1) end |
size
Also known as: length
Returns the real size of the table (number of entries + number of elements in array side)
215 216 217 218 |
# File 'lib/rufus/lua/objects.rb', line 215 def size self.to_h.size end |
to_a
Returns a Ruby Array instance representing this Lua table.
Will raise an error if the ‘rendering’ is not possible.
s = Rufus::Lua::State.new @s.eval("return { a = 'A', b = 'B', c = 3 }").to_a # => error ! @s.eval("return { 1, 2 }").to_a # => [ 1.0, 2.0 ] @s.eval("return {}").to_a # => [] @s.eval("return { 1, 2, car = 'benz' }").to_a # => error !
to_a(false)
Setting the optional argument ‘pure’ to false will manage any table :
s = Rufus::Lua::State.new @s.eval("return { a = 'A', b = 'B', c = 3 }").to_a(false) # => [["a", "A"], ["b", "B"], ["c", 3.0]] @s.eval("return { 1, 2 }").to_a(false) # => [1.0, 2.0] @s.eval("return {}").to_a(false) # => [] @s.eval("return { 1, 2, car = 'benz' }").to_a(false) # => [1.0, 2.0, ["car", "benz"]]
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/rufus/lua/objects.rb', line 285 def to_a (pure=true) h = self.to_h pure && h.keys.find { |k| not [ Float ].include?(k.class) } && raise("cannot turn hash into array, some keys are not numbers") a_keys = (1..objlen).to_a.collect { |k| k.to_f } keys = a_keys + (h.keys - a_keys) keys.inject([]) { |a, k| a << (a_keys.include?(k) ? h[k] : [ k, h[k] ]) a } end |
to_h
Returns a Ruby Hash instance representing this Lua table.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/rufus/lua/objects.rb', line 223 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) value.load_onto_stack if value.is_a?(Ref) key = stack_fetch(-2) key.load_onto_stack if key.is_a?(Ref) stack_unstack # leave key on top h[key] = value end h end # Returns a Ruby Array instance representing this Lua table. # # Will raise an error if the 'rendering' is not possible. # # s = Rufus::Lua::State.new # # @s.eval("return { a = 'A', b = 'B', c = 3 }").to_a # # => error ! # # @s.eval("return { 1, 2 }").to_a # # => [ 1.0, 2.0 ] # # @s.eval("return {}").to_a # # => [] # # @s.eval("return { 1, 2, car = 'benz' }").to_a # # => error ! # # == to_a(false) # # Setting the optional argument 'pure' to false will manage any table : # # s = Rufus::Lua::State.new # # @s.eval("return { a = 'A', b = 'B', c = 3 }").to_a(false) # # => [["a", "A"], ["b", "B"], ["c", 3.0]] # # @s.eval("return { 1, 2 }").to_a(false) # # => [1.0, 2.0] # # @s.eval("return {}").to_a(false) # # => [] # # @s.eval("return { 1, 2, car = 'benz' }").to_a(false) # # => [1.0, 2.0, ["car", "benz"]] # def to_a (pure=true) h = self.to_h pure && h.keys.find { |k| not [ Float ].include?(k.class) } && raise("cannot turn hash into array, some keys are not numbers") a_keys = (1..objlen).to_a.collect { |k| k.to_f } keys = a_keys + (h.keys - a_keys) keys.inject([]) { |a, k| a << (a_keys.include?(k) ? h[k] : [ k, h[k] ]) a } end # Turns the Lua table into a Ruby array, or else into a Ruby Hash instance. # def to_ruby to_a rescue to_h end end |
to_ruby
Turns the Lua table into a Ruby array, or else into a Ruby Hash instance.
303 304 305 306 |
# File 'lib/rufus/lua/objects.rb', line 303 def to_ruby to_a rescue to_h end |
values
Returns the array of values in this Table.
171 172 173 174 |
# File 'lib/rufus/lua/objects.rb', line 171 def values self.to_h.values end |