Skip to content
instance method

Enumerator#with_object

[U] (U obj) { (Elem, U obj) -> untyped } -> U
[U] (U obj) -> ::Enumerator[[ Elem, U ], U]
e.with_object(obj) {|(*args), obj| ... }
e.with_object(obj)

Alias of #each_with_object

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

If no block is given, returns a new Enumerator.

Example

Example
to_three = Enumerator.new do |y|
  3.times do |x|
    y << x
  end
end

to_three_with_string = to_three.with_object("foo")
to_three_with_string.each do |x,string|
  puts "#{string}: #{x}"
end

# => foo: 0
# => foo: 1
# => foo: 2
See also #each_with_object #with_index #size also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0