From: Robert Dober Date: 2008-11-30T19:11:00+09:00 Subject: Re: Enumerable#select used to return actual values On Sun, Nov 30, 2008 at 8:54 AM, Robert Klemme wrote: > On 30.11.2008 04:46, Mike Austin wrote: >> >> I'm pretty sure select used to use the actual value of the called block, >> instead of treating it as just a truthy/falsey boolean. > > I am pretty sure it did never. Maybe you have a combination with to_enum in > mind or rather inject? > >> I know this is unconventional, but it's a really nice, compact way of >> expressing map and filter in one shot, like list comprehension: >> >> (1..10).select { |a| a * a if a > 5 } >> >> Was it ever there, or did I just have a dream about it? It's easy to >> implement... just wondering. > > Maybe 1.9 has a nifty trick, I don't use it that often yet. > > As said, inject is always a good candidate for this: > > irb(main):005:0> (1..10).inject([]) {|r,a| r << (a*a) if a > 5; r} As Robert was the first to suggest inject I am left with the less attractive joices ;) a.map{ |a| a > 5 ? a*a: nil}.compact # does not work for any &blk of course or myselection = proc{ |a| a*a if a > 5} a.select(&myselection).map(&myselection) or module Enumerable def map_selection &blk select( &blk ).map( &blk ) end end HTH Robert -- Ne baisse jamais la tête, tu ne verrais plus les étoiles. Robert Dober ;)