From: Martin DeMello Date: 2003-07-31T02:52:21+09:00 Subject: collect_by For all you FPers out there - is this a defined function in any language's standard library? And if so, what is it called? module Enumerable def collect_by h = {} each {|i| j = yield i (h[j] ||= []) << i } h # or, as a one-liner # inject({}) {|c, e| (c[yield e] ||= []) << e; c} end end a = [1,2,3,4,5,6,7,8,9,10] p a.collect_by {|i| i % 2} #=> {0=>[2, 4, 6, 8, 10], 1=>[1, 3, 5, 7, 9]} martin