End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Enumerable#each_entry
each_entry(*args) {|element| ... } → self
each_entry(*args) → enumerator
enum.c
View on GitHub ↗
static VALUE
enum_each_entry(int argc, VALUE *argv, VALUE obj)
{
RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
rb_block_call(obj, id_each, argc, argv, each_val_i, 0);
return obj;
}
Calls the given block with each element, converting multiple values from yield to an array; returns self:
Example
a = [] (1..4).each_entry {|element| a.push(element) } # => 1..4 a # => [1, 2, 3, 4] a = [] h = {foo: 0, bar: 1, baz:2} h.each_entry {|element| a.push(element) } # => {:foo=>0, :bar=>1, :baz=>2} a # => [[:foo, 0], [:bar, 1], [:baz, 2]] class Foo include Enumerable def each yield 1 yield 1, 2 yield end end Foo.new.each_entry {|yielded| p yielded }
Output:
Example
1 [1, 2] nil
With no block given, returns an Enumerator.
See also
#each_cons
#each_slice
#drop_while
#each_with_index
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0