From: Jacob Fugal Date: 2005-08-06T01:39:54+09:00 Subject: Re: Yet Another useless Ruby 2 Idea On 8/5/05, Daniel Brockman wrote: > > items.cool_method_name {|item| item*2 if item > 2} > > > > is quite doable in ruby > > Except you can't collect nil values that way. With that syntax, no. This would be a great place for multiple block arguments though: module Enumerable def filter_and_map( &filter, &map ) result = [] self.each { |el| result << map[el] if filter[el] } return result end end [ 1, 2, 3, 4 ].filter_and_map { |i| i % 2 == 0 } { |i| i ** 2 } # => [ 4, 16 ] Currently, this would have to written (the change to the method implementation follows easily) [ 1, 2, 3, 4 ].filter_and_map( lambda{ |i| ... }, lambda{ |i| ... } ) Although, once we have named parameters and have settled on a syntax for lambdas, it wouldn't be so bad: [ 1, 2, 3, 4 ].filter_and_map( filter: { |i| ... }, map: { |i| ... }, ) I'm with many of the other posters in this thread, however, and find the list comprehension syntax even better[1]. Oh, well. Jacob Fugal [1] I think that ary.select{ |i| ... }.map{ |i| ... } may be even more readable than the list comprehension, but it suffers performance due to two passes through each of the accepted elements.