APIdock / Ruby
/
method

each_pair

ruby latest stable - Class: Struct
each_pair()
public

Yields the name and value of each struct member in order. If no block is given an enumerator is returned.

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
static VALUE
rb_struct_each_pair(VALUE s)
{
 VALUE members;
 long i;
 RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
 members = rb_struct_members(s);
 if (rb_block_arity() > 1) {
 for (i=0; i<RSTRUCT_LEN(s); i++) {
 VALUE key = rb_ary_entry(members, i);
 VALUE value = RSTRUCT_GET(s, i);
 rb_yield_values(2, key, value);
 }
 }
 else {
 for (i=0; i<RSTRUCT_LEN(s); i++) {
 VALUE key = rb_ary_entry(members, i);
 VALUE value = RSTRUCT_GET(s, i);
 rb_yield(rb_assoc_new(key, value));
 }
 }
 return s;
}

AltStyle によって変換されたページ (->オリジナル) /