From: Kristof Bastiaensen Date: 2004-05-19T20:38:56+09:00 Subject: Re: RCR: More enumerator functionality On Wed, 19 May 2004 11:47:30 +0900, nobu.nokada wrote: >> 5.enum_filter(:times){ |i| i[0] == 0 }.collect >> => [2, 4] > => [0, 2, 4] > > Isn't it? > Oh, yes, of course. (stupid mistake) >> (2)* Enumerable::Enumerator takes a optional block >> >> powers = 4.enum_for(:times){ |i| i * i } >> powers.collect >> => [0, 1, 4, 9] > > I tried the implementation of these two experimentally. But I > feel enum_filter is overdoing after all. > > This patch provides block for Enumerable#enum_for and > Enumerable#enum_if instead. Yes, that's cleaner. I like the naming also. > > $ ./ruby -renumerator -e 'p 5.enum_for(:times){|i| i*i}.to_a' > [0, 1, 4, 9, 16] > $ ./ruby -renumerator -e 'p 5.enum_for(:times).enum_if{|i| i[0] == 0}.to_a' > [0, 2, 4] > > Just my 2 yens. > Great! Thanks, Kristof