Hi --

On Sun, 30 Nov 2008, 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.

(0..10).each_with_object([]) {|e,a| a << (e * e) if e > 5 }

Basically inject with a sticky accumulator.


David

-- 
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS (Jan 12-15) | ADVANCING WITH RAILS (Jan 19-22) *
Both in Fort Lauderdale, FL      * Co-taught with Patrick Ewing
See http://www.rubypal.com for details
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)