Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Enumerable#each_with_object

enum.each_with_object(obj) { |(*args), memo_obj| ... } → obj
enum.each_with_object(obj) → an_enumerator

Iterates the given block for each element with an arbitrary object given, and returns the initially given object.

If no block is given, returns an enumerator.

Example
evens = (1..10).each_with_object([]) { |i, a| a << i*2 }
#=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
See also #each_with_index #entries #each_slice #filter also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1