# 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

each()
Alias for: each_pair
Lua::Table.each_index { |key| block } → Lua::Table click to toggle source
Lua::Table.each_ikey { |key| block } → Lua::Table

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

Also aliased as: each_index
each_index()
Alias for: each_ikey
Lua::Table.each_ipair { |key,value| block } → Lua::Table click to toggle source

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

Lua::Table.each_ivalue { |value| block } → Lua::Table click to toggle source

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

Lua::Table.each_key { |key| block } → Lua::Table click to toggle source

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

Lua::Table.each { |key,value| block } → Lua::Table click to toggle source
Lua::Table.each_pair { |key,value| block } → Lua::Table

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

Also aliased as: each
Lua::Table.each_value { |value| block } → Lua::Table click to toggle source

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

Lua::Table.to_array → Array click to toggle source

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

Lua::Table.to_hash → Hash click to toggle source

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