Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Struct#each_pair
() -> ::Enumerator[[ ::Symbol, E ], self]
() { ([ ::Symbol, E ] key_value) -> void } -> self
each_pair {|(name, value)| ... } → self
each_pair → enumerator
struct.c
View on GitHub ↗
static VALUE
rb_struct_each_pair(VALUE s)
{
VALUE members;
rb_len_t i;
RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
members = rb_struct_members(s);
if (rb_block_pair_yield_optimizable()) {
for (i=0; i<RSTRUCT_LEN_RAW(s); i++) {
VALUE key = rb_ary_entry(members, i);
VALUE value = RSTRUCT_GET_RAW(s, i);
rb_yield_values(2, key, value);
}
}
else {
for (i=0; i<RSTRUCT_LEN_RAW(s); i++) {
VALUE key = rb_ary_entry(members, i);
VALUE value = RSTRUCT_GET_RAW(s, i);
rb_yield(rb_assoc_new(key, value));
}
}
return s;
}
Calls the given block with each member name/value pair; returns self:
Example
Customer = Struct.new(:name, :address, :zip) # => Customer joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each_pair {|(name, value)| p "#{name} => #{value}" }
Output:
Example
"name => Joe Smith" "address => 123 Maple, Anytown NC" "zip => 12345"
Returns an Enumerator if no block is given.
Related: each.