On Sunday, July 20, 2003, 2:56:08 AM, Yukihiro wrote: > Hi, > In message "Re: Proposal: Array#to_h, to simplify hash generation" > on 03/07/20, Gavin Sinclair <gsinclair / soyabean.com.au> writes: > |I thought it sounded familiar, but didn't see an RCR. > I don't remember the RCR number. Search for "hashify". It's #12. Interesting: I like the #hashify idea better than my proposal. My original code could be written # return { filename -> size } def get_local_gz_files Dir["*.gz"].to_hash { |filename| File.stat(filename).size } end That does away with the intermediate assoc array, and is overall very elegant. Best of all, it can be used with any Enumerable type, and it doesn't have any requirement on the structure of the receiver. module Enumerable def to_hash result = {} each do |elt| result[elt] = yield(elt) end result end end That is capturing the very idiom I have repeated so many times. Alternatives to #to_hash are: hashify (the original and the worst :) map_hash hash_map (it is, after all, mapping a collection into a hash) I think I like "map_hash" the best. ["cat", "dog", "mouse"].map { |s| s.length } # -> [3, 3, 5] ["cat", "dog", "mouse"].map_hash { |s| s.length } # -> {"cat"=>3, "mouse"=>5, "dog"=>3} Gavin