Class: Lua::Table

Inherits:
_RefObject
  • Object
show all
Defined in:
rubyluabridge.cpp

Overview

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.

***************************************************************************

Instance Method Summary (collapse)

Instance Method Details

- (Object) Lua::Table.each_index {|key| ... } - (Object) Lua::Table.each_ikey {|key| ... }

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.

Overloads:

  • - (Object) Lua::Table.each_index {|key| ... }

    Yields:

    • (key)
  • - (Object) Lua::Table.each_ikey {|key| ... }

    Yields:

    • (key)


# File 'rubyluabridge.cpp'

/* call-seq: 
 *      Lua::Table.each_index { |key| block } -> Lua::Table
 *      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;
}

- (Object) Lua::Table.each_ipair {|key, value| ... }

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.

Yields:

  • (key, value)


# File 'rubyluabridge.cpp'

/* call-seq: 
 *      Lua::Table.each_ipair { |key,value| block } -> Lua::Table
 *
 * 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;
}

- (Object) Lua::Table.each_ivalue {|value| ... }

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.

Yields:

  • (value)


# File 'rubyluabridge.cpp'

/* call-seq: 
 *      Lua::Table.each_ivalue { |value| block } -> Lua::Table
 *
 * 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;
}

- (Object) Lua::Table.each_key {|key| ... }

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.

Yields:

  • (key)


# File 'rubyluabridge.cpp'

/* call-seq: 
 *      Lua::Table.each_key { |key| block } -> Lua::Table
 *
 * 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;
}

- (Object) Lua::Table.each {|key, value| ... } - (Object) Lua::Table.each_pair {|key, value| ... }

Calls block once for each key in table, passing the key and value as parameters.

This goes over all pairs in the table.

Overloads:

  • - (Object) Lua::Table.each {|key, value| ... }

    Yields:

    • (key, value)
  • - (Object) Lua::Table.each_pair {|key, value| ... }

    Yields:

    • (key, value)


# File 'rubyluabridge.cpp'

/* call-seq: 
 *      Lua::Table.each { |key,value| block } -> Lua::Table
 *      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;
}

- (Object) Lua::Table.each_value {|value| ... }

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.

Yields:

  • (value)


# File 'rubyluabridge.cpp'

/* call-seq: 
 *      Lua::Table.each_value { |value| block } -> Lua::Table
 *
 * 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;
}

- (Object) Lua::Table.to_array

Returns a Ruby Array of the (first, dense) integer keys in the Table.



# File 'rubyluabridge.cpp'

/* call-seq: 
 *        Lua::Table.to_array -> Array
 *
 * 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;
}

- (Object) Lua::Table.to_hash

Returns a Ruby Hash of all pairs in the table.



# File 'rubyluabridge.cpp'

/* call-seq: 
 *        Lua::Table.to_hash -> Hash
 *
 * 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;
}