From: Yossef Mendelssohn Date: 2010-09-04T00:40:01+09:00 Subject: Re: Enumerator#each return value is surprising On Sep 3, 10:12 am, Christopher Dicely wrote: > I suppose when the enumerator is created with the default options, I > can sort of see that (or at least, see it as unlikely to be harmful.) > When its created with enum_for with an argument other than :each, > though, it seems to me like you end up with more possibility for > unexpected results. > > e.g., > > array = ["a","b","c"] > enum = array.enum_for(:each_with_index) > enum.each {} # => ["a","b","c"] > enum.to_a.each {} # => [["a", 0], ["b", 1], ["c", 2]] > > That is, even ignoring the implementation class, the sequence returned > by Enumerator#each isn't always the same sequence that the Enumerator > represents. This isn't like any other Enumerable in core Ruby. First system ruby 1.8.6, then rvm with 1.9.1 $ irb >> %w[a b c].each {} => ["a", "b", "c"] >> %w[a b c].each_with_index {} => ["a", "b", "c"] >> exit $ rvm use ruby-1.9.1 $ irb >> %w[a b c].each => # >> _.each {} => ["a", "b", "c"] > %w[a b c].each {} => ["a", "b", "c"] > %w[a b c].each_with_index {} => ["a", "b", "c"] > %w[a b c].each_with_index => # > _.each {} => ["a", "b", "c"] You should learn to expect the original array as a return value. It's not about what kind of `each` enumeration you use, but the ability to chain calls as expected. If you want to get something different, use a transforming enumerator instead of `each`. -- -yossef