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