On Sunday 03 February 2013 Arup Rakshit wrote
> Stefano Crocco wrote in post #1094920:
> > On Sunday 03 February 2013 Arup Rakshit wrote
> 
> @Stefano ->
> 
> Could you simulate the same example with the enum#each method, just to
> understand the difference between enum#each and array#each?
> 
> 
> Thanks
> 
> --
> Posted via http://www.ruby-forum.com/.

Again, there's no difference, since Enumerator#each (when the Enumerator was 
created from an array) calls Array.each.

To clarify things, imagine we create a new class, MyArray, which contains an 
array (to make things simple, we pass the array to the constructor). This 
class has an #each method which takes a block (using the &blk syntax), then 
displays the text "Calling MyArray#each", then calls the #each method of the 
internal array:


class MyArray

  def initialize a
    @array = a
  end
  
  def each &blk
    puts "Calling MyArray#each"
    @array.each &blk
  end
  
end

Now, we create an instance of MyArray, and create an enumerator from it.

ma = MyArray.new [1,2,3,4,5]
e = ma.to_enum

Now, try calling the each method once on ma and once on e, and you'll see that 
they produce the same output:

ma.each{|i| puts i}
e.each{|i| puts i}

In both cases, the output is:

Calling MyArray#each
1
2
3
4
5
=> [1, 2, 3, 4, 5]

As you can see, there's no difference between the two calls.

Stefano