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
Show source
Register or log in to add new notes.
February 3, 2013 - (v1_9_3_125)
0 thanks

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:

http://stackoverflow.com/questions/14671881/how-does-enumwith-object-differ-from-enumeach-with-object/14672305#14672305