Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Struct#each
() -> ::Enumerator[Elem, self]
() { (Elem value) -> void } -> self
each {|value| ... } → self
each → enumerator
struct.c
View on GitHub ↗
static VALUE
rb_struct_each(VALUE s)
{
long i;
RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
for (i=0; i<RSTRUCT_LEN(s); i++) {
rb_yield(RSTRUCT_GET(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.