From: Stefano Crocco Date: 2013-02-03T06:00:06+09:00 Subject: Re: Why none of the block giving the expected output with the "enumerator"? On Sunday 03 February 2013 Arup Rakshit wrote > Stefano Crocco wrote in post #1094918: > > On Sunday 03 February 2013 Arup Rakshit wrote > > > >> => [11, 22, 31, 224, 44] > >> > >> from (irb):22:in `-' > >> from (irb):22:in `block in irb_binding' > >> from (irb):22:in `each' > >> from (irb):22:in `each' > >> from (irb):22 > >> from C:/Ruby193/bin/irb:12:in `
' > >> > >> irb(main):023:0> > > @Stefano why is it calling Array's "each" method,instead of > "enum#each",as i converted it to enumerator? > > thanks > > -- > Posted via http://www.ruby-forum.com/. Because Enumerator#each, in turn, calls the each method of the object it was created from. It is (more or less) if the enumerator was created this way: class Enumerator def initialize object, method = :each @object = object @method = method @args = args end def each @object.send :method, *args end end Actually, Enumerator is written in C, so its implementation is different from this. So, when you create an enumerator from an array, the #each method of the Enumerator calls the #each method of the array. Stefano