From: "David A. Black" Date: 2008-08-06T10:44:06+09:00 Subject: [ruby-core:18138] Re: Enumerators that know about a block Hi -- On Wed, 6 Aug 2008, Shugo Maeda wrote: > Hi, > > 2008/8/1 David A. Black : >> I'm remembering that at one point it was possible to do this: >> >> array = [1,2,3] >> func = lambda {|x| x * 10 } >> e = array.enum_for(:map, &func) >> e.next >> => 10 >> >> But now the result is 1, because &func is ignored. >> >> I'm (a) puzzled by this, since it seems to me that enumerators are >> much more useful if they can remember a block, and (b) wondering if >> there's some similar replacement technique that I haven't managed to >> figure out. > > enum_for is designed to apply a block for the specified method > (Array#map in this case) by later Enumerator method calls, so it > simply ignores a given block. > > You may need the lazy version of Enumerable#map that returns > Enumerator instead of Array. > > e = [1, 2, 3].lazy_map {|x| x * 10} > p e.next #=> 10 > p e.next #=> 20 Is that an actual method (I couldn't find it anywhere) or just a hypothetical example? I'm still wondering why enumerators aren't designed to do that automatically if they're given a block upon initialization. David -- Rails training from David A. Black and Ruby Power and Light: * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!