class Lua::Table
When Rua marshals a Lua table to Ruby, it instantiates it as a Lua::Table.
This gives it some extra methods that aren't available to a regular Lua::RefObject.
Public Instance Methods
Calls block once for each integer key in table, passing the key to the block as a parameter.
This goes over all integer pairs in the table. Similar to ipairs(), this only touches the first table integers, thus treating the table like a dense array.
VALUE rlua_Table_each_ikey( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int tablelen = lua_objlen( L, -1 ); int i; for ( i = 1; i <= tablelen; ++i ) { lua_rawgeti( L, -1, i ); rb_yield( INT2NUM(i) ); lua_pop( L, 1 ); } lua_pop( L, 1 ); return self; }
Calls block once for each integer key in table, passing the key and value as parameters.
This goes over all integer pairs in the table. Similar to ipairs(), this only touches the first table integers, thus treating the table like a dense array.
VALUE rlua_Table_each_ipair( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int tablelen = lua_objlen( L, -1 ); int i; for ( i = 1; i <= tablelen; ++i ) { lua_rawgeti( L, -1, i ); VALUE rvalue = marshal_lua_to_ruby( pRefObject->Rstate, L, -1 ); rb_yield_values( 2, INT2NUM(i), rvalue ); lua_pop( L, 1 ); } lua_pop( L, 1 ); return self; }
Calls block once for each integer key in table, passing the value to the block as a parameter.
This goes over all integer pairs in the table. Similar to ipairs(), this only touches the first table integers, thus treating the table like a dense array.
VALUE rlua_Table_each_ivalue( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int tablelen = lua_objlen( L, -1 ); int i; for ( i = 1; i <= tablelen; ++i ) { lua_rawgeti( L, -1, i ); VALUE rvalue = marshal_lua_to_ruby( pRefObject->Rstate, L, -1 ); rb_yield( rvalue ); lua_pop( L, 1 ); } lua_pop( L, 1 ); return self; }
Calls block once for each key in table, passing the key to the block as a parameter.
This goes over all pairs in the table.
VALUE rlua_Table_each_key( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); // table is in the stack at index 't' lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int table = lua_gettop( L ); lua_pushnil( L ); // first key while ( lua_next(L, table) != 0 ) { // uses 'key' (at index -2) and 'value' (at index -1) VALUE rkey = marshal_lua_to_ruby( pRefObject->Rstate, L, -2 ); rb_yield( rkey ); // removes 'value'; keeps 'key' for next iteration lua_pop( L, 1 ); } lua_pop( L, 1 ); return self; }
Calls block once for each key in table, passing the key and value as parameters.
This goes over all pairs in the table.
VALUE rlua_Table_each_pair( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); // table is in the stack at index 't' lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int table = lua_gettop( L ); lua_pushnil( L ); // first key while ( lua_next(L, table) != 0 ) { // uses 'key' (at index -2) and 'value' (at index -1) VALUE rvalue = marshal_lua_to_ruby( pRefObject->Rstate, L, -1 ); VALUE rkey = marshal_lua_to_ruby( pRefObject->Rstate, L, -2 ); rb_yield_values( 2, rkey, rvalue ); // removes 'value'; keeps 'key' for next iteration lua_pop( L, 1 ); } lua_pop( L, 1 ); return self; }
Calls block once for each key in table, passing the value to the block as a parameter.
This goes over all pairs in the table.
VALUE rlua_Table_each_value( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); // table is in the stack at index 't' lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int table = lua_gettop( L ); lua_pushnil( L ); // first key while ( lua_next(L, table) != 0 ) { // uses 'key' (at index -2) and 'value' (at index -1) VALUE rvalue = marshal_lua_to_ruby( pRefObject->Rstate, L, -1 ); rb_yield( rvalue ); // removes 'value'; keeps 'key' for next iteration lua_pop( L, 1 ); } lua_pop( L, 1 ); return self; }
Returns a Ruby Array of the (first, dense) integer keys in the Table.
VALUE rlua_Table_to_array( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int tablelen = lua_objlen( L, -1 ); VALUE array = rb_ary_new2( tablelen ); int i; for ( i = 1; i <= tablelen; ++i ) { lua_rawgeti( L, -1, i ); VALUE rvalue = marshal_lua_to_ruby( pRefObject->Rstate, L, -1 ); rb_ary_push( array, rvalue ); lua_pop( L, 1 ); } lua_pop( L, 1 ); return array; }
Returns a Ruby Hash of all pairs in the table.
VALUE rlua_Table_to_hash( VALUE self ) { rlua_RefObject* pRefObject; Data_Get_Struct( self, rlua_RefObject, pRefObject ); lua_State* L = pRefObject->getState(); VALUE hash = rb_hash_new(); // table is in the stack at index 't' lua_rawgeti( L, LUA_REGISTRYINDEX, pRefObject->Lref ); int table = lua_gettop( L ); lua_pushnil( L ); // first key while ( lua_next(L, table) != 0 ) { // uses 'key' (at index -2) and 'value' (at index -1) VALUE rvalue = marshal_lua_to_ruby( pRefObject->Rstate, L, -1 ); VALUE rkey = marshal_lua_to_ruby( pRefObject->Rstate, L, -2 ); rb_hash_aset( hash, rkey, rvalue ); // removes 'value'; keeps 'key' for next iteration lua_pop( L, 1 ); } lua_pop( L, 1 ); return hash; }