There is an Enumerable#reject method that serves this purpose only:
(1..4).reject{|x| x == 3}.collect{|x| x + 1}
The practice of directly using the output of one method as the input of another is called the method chain and is very common in Ruby.
BTW, map (or collect ) is used to directly display the input enumerated on the output. If you need to output a different number of elements, perhaps you need a different Enumerable method.
Edit: If you are concerned about the fact that some of the elements are repeated twice, you can use a less elegant inject based inject (or its similar method called each_with_object ):
(1..4).each_with_object([]){|x,a| a << x + 1 unless x == 3}
Mladen JablanoviΔ Mar 01 '11 at 9:11 2011-03-01 09:11
source share