method
with_object
v1_9_3_125 -
Show latest stable
- Class:
Enumerator
with_object(p1)public
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
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
1Note
Difference between enum#with_object and enum#each_with_object
I found a very good post on +SO+ - which clearly explained the difference between enum#with_object and enum#each_with_object. The link is as follows: