Hi --
From: David A. Black [mailto:dblack@rubypal.com]
..
#For a little while, 1.9 allowed you to do this:
#>> e = [1,2,3].enum_for(:map, &lambda {|x| x * 10 })
#=> #<Enumerable::Enumerator:0x39038c>
#>> e.select {|n| n > 10 }
#=> [20, 30]
#But #enum_for no longer accepts a &block-style argument. The same code
#in current 1.9 would give you , because it would ignore the lambda
#and just do a pass-through mapping.
#I'm still unclear as to why this behavior was removed. It seems to me
#that its removal reduces the usefulness of enumerators drastically.
maybe because it does not look any better than the simpler (but less efficient)
e = [1,2,3].select{|n| n>1}.map{|x| x*10}
=>[20,30]
It's not really about the visual appearance of this or that example,
though; it's more about efficiency, since that's supposed to be one of
the big advantages of enumerators. The other advantage is that you can
call next and rewind... but you cannot, any longer, do this:
irb(main):007:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):008:0> e = a.enum_for(:map, &lambda {|x| x * 10 })
=> #<Enumerable::Enumerator:0x3d7444>
irb(main):009:0> e.next
=> 10
You'll get 1 now, rather than 10. I'm still very puzzled by the
removal of this capability.
i was actually dreaming of ruby supporting multiple blocks
so that you can do simply
e = [1,2,3].select{|n| n>1},{|x| x*10}
=>[20,30]
meaning, do a (re)map if a second code block is passed
That comes up periodically.... It never appealed to me; I guess I
think of it like a method having two argument lists. Besides, how
would you distinguish it from a hash literal? 
David
···
On Fri, 12 Sep 2008, Peña, Botp wrote:
--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!