In Ruby, is there an Array method that combines 'select' and 'map'?

ruby

Solution

I usually use `map` and `compact` together along with my selection criteria as a postfix `if`. `compact` gets rid of the nils.

jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}    
 => [3, 3, 3, nil, nil, nil] 


jruby-1.5.0 > [1,1,1,2,3,4].map{|n| n*3 if n==1}.compact
 => [3, 3, 3] 

Problem

I have a Ruby array containing some string values. I need to: - Find all elements that match some predicate - Run the matching elements through a transformation - Return the results as an array Right now my solution looks like this: ``` def example matchingLines = @lines.select{ |line| ... } results = matchingLines.map{ |line| ... } return results.uniq.sort end ``` Is there an Array or Enumerable method that combines select and map into a single logical statement?

Original source

Related problems