From: David Vallner Date: 2006-02-09T11:10:45+09:00 Subject: Re: can map/collect return fewer values than are found in the target? The difference seems to be that Ruby's interpretation of map seems to be stricter than Perl's, e.g. for one item in the original Array, there's exactly one corresponding item in the result. If you want a method with the behaviour similar to Perl's, you could do: class Array def pl_map(&block) map(&block).compact.flatten end end new_items = items.pl_map { | item | item if VOWELS.include(item) } David Vallner Dňa Štvrtok 09 Február 2006 02:18 Kelly Dwight Felkins napísal: > Is there a way for collect to return fewer items than the enumerable the > collect is running on? It looks to me like > map/collect will return at least one value for each item in the array -- > I've seen examples of how to return more items, but > not less. > > Is there a way to do this more succinctly: > > VOWELS = %w{a e i o u} > items = %w{ a b c d e f g h i} > new_items = [] > items.each do |item| > new_items << item unless VOWELS.include(item) > end > new_items > > map/collect in ruby seems very similar to map in perl except that in perl > the block can return zero or more elements. Is there an method in ruby that > is similar to perl's map? > > Thanks, > > -Kelly