From: Gavin Sinclair Date: 2003-07-21T15:25:56+09:00 Subject: Re: Proposal: Array#to_h, to simplify hash generation > > To me, 'hashify' implies taking an assoc array and converting it to hash > form (or perhaps the perl-influenced [a, b, c, d] -> {a=>b, c=>d}). I > still can't think of a name for the useful case :) make_hash, perhaps .. > > *(1..10).make_hash {|i| f(i)} > > or maybe the complementary hash_to and hash_from, where the block is > respectively the value and the key for the corresponding array entry :) > > martin I like "make_hash". We would get more flexibility if "make_hash" insisted on receiving two values for the block: one for key and one for value. In one instance recently, I wanted to map the "filename" part of a data object to the object itself. This, I think, is readable: map = receipts.make_hash { |r| r.filename, r } Whereas in my pet case of mapping filename to size, we have map = filenames.make_hash { |fn| fn, File.stat(fn).size } And your example comes out as (1..10).make_hash { |i| i, f(i) } (I don't know why you put the asterix there; Ranges are Enumerable.) And, of course, the obligitory reference implementation: module Enumerable def make_hash result = {} each do |elt| key, value = yield(elt) result[key] = value end result end end I've raised an RCR for this (#148). Cheers, Gavin