From: Bruce Woodward Date: 2006-04-13T09:30:20+09:00 Subject: map_if, collect_if ??? ------=_Part_12194_33033969.1144888215618 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi, I have been through the pickaxe book, looking at the array and enumerable built-in class and module and can't find a way of achieving a map_if or collect_if. From pickaxe, collect "Returns a new array containing the results of runnin= g block once for every element in enum". Of course map is an alias to collect= . There is find_all but it returns an array containing the elements of enum, not the results of running the block, so I have written one. Is there a better way that already exists? thanks, Bruce. module Enumerable # returns an array containing the results of running block once for every element in enum, for which block # is not false. def map_if rt, r =3D [], nil self.each { |e| (r =3D yield(e)) and rt << r } rt end alias :collect_if :map_if end a =3D [ 1,2,3,4,5 ] b =3D a.map_if { |i| (i % 2 !=3D 0) and (i * 10) } p b ## [10, 30, 50] ------=_Part_12194_33033969.1144888215618--