From: Kristof Bastiaensen Date: 2004-04-09T10:04:26+09:00 Subject: new method(?) collect_by Hi, I have written a method collect_by. I am not sure if it already exist, but it is very usefull. It is a generic version of collect, that works with any method. Basicly it converts the output of any block to an Array class Object def collect_by(method, *args) a = [] send(method, *args) { |*o| a.push(block_given? ? (yield *o) : (o.size > 1 ? o : o[0])) } return a end end Examples: "a string".collect_by(:each_byte) => [97, 32, 115, 116, 114, 105, 110, 103] 3.collect_by(:step, 20, 4) =>[3, 7, 11, 15, 19] (3..7).collect_by(:each_with_index) => [[3, 0], [4, 1], [5, 2], [6, 3], [7, 4]] 5.collect_by(:times) { rand 100 } => [61, 85, 13, 52, 47] 5.collect_by(:times) { |i| i * i } => [0, 1, 4, 9, 16]