From: Phrogz Date: 2006-11-28T13:45:14+09:00 Subject: Re: ruby and list comprehension James Cunningham wrote: > I found this while web searching for the same thing recently; I can't > recall where I found it. It's a cute little hack. > > class Array > def comprehend > return self unless block_given? > result = [] > self.each { |i| result.push yield(i) } > result.compact > end > end Maybe I don't comprehend comprehending, but why the result/each instead of map? class Array def comprehend if block_given? map{ |i| yield( i ) }.compact else self end end end or perhaps better class Array def comprehend( &block ) block ? map( &block ).compact : self end end