Method: Struct#each_pair

Defined in:
struct.c

#each_pair {|sym, obj| ... } ⇒ Object #each_pairObject

Calls block once for each instance variable, passing the name (as a symbol) and the value as parameters.

If no block is given, an enumerator is returned instead.

Customer = Struct.new(:name, :address, :zip)
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe.each_pair {|name, value| puts("#{name} => #{value}") }

produces:

name => Joe Smith
address => 123 Maple, Anytown NC
zip => 12345

Overloads:

  • #each_pair {|sym, obj| ... } ⇒ Object

    Yields:

    • (sym, obj)
[View source]

# File 'struct.c'

/*
 *  call-seq:
 *     struct.each_pair {|sym, obj| block }     -> struct
 *     struct.each_pair                         -> an_enumerator
 *
 *  Calls <i>block</i> once for each instance variable, passing the name
 *  (as a symbol) and the value as parameters.
 *
 *  If no block is given, an enumerator is returned instead.
 *
 *     Customer = Struct.new(:name, :address, :zip)
 *     joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
 *     joe.each_pair {|name, value| puts("#{name} => #{value}") }
 *
 *  <em>produces:</em>
 *
 *     name => Joe Smith
 *     address => 123 Maple, Anytown NC
 *     zip => 12345
 */

static VALUE
rb_struct_each_pair(VALUE s)
{
    VALUE members;
    long i;

    RETURN_ENUMERATOR(s, 0, 0);
    members = rb_struct_members(s);
    for (i=0; i<RSTRUCT_LEN(s); i++) {
    rb_yield_values(2, rb_ary_entry(members, i), RSTRUCT_PTR(s)[i]);
    }
    return s;
}