From: "marcandre (Marc-Andre Lafortune)" Date: 2012-07-22T06:51:39+09:00 Subject: [ruby-core:46599] [ruby-trunk - Feature #6669] A method like Hash#map but returns hash Issue #6669 has been updated by marcandre (Marc-Andre Lafortune). Hi, duerst (Martin D��rst) wrote: > Wouldn't it be really confusing that for Arrays, #map and #collect are > synonyms, but for Hash, they are different? It could be confusing, and would also introduce incompatibilities. Also, if the proposal for associate/categorize is accepted, then this `collect` would be a duplication. I believe that methods like associate/categorize acting on all Enumerable to produce Hashes would be far more useful (since they don't only act on hashes), without introducing incompatibilities. ---------------------------------------- Feature #6669: A method like Hash#map but returns hash https://bugs.ruby-lang.org/issues/6669#change-28264 Author: yhara (Yutaka HARA) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: core Target version: 2.0.0 =begin Given a hash h, h.map returns an array(alist), but sometimes I hope it returned a hash. Example: class Hash def apply(&block) self.inject({}) do |h, (k, v)| new_k, new_v = *block.call(k, v) h[new_k] = new_v h end end end score = { taro: [1,3,2], jiro: [3,5,8,4], saburo: [2,9] } max_score = score.apply{|k,v| [k, v.max]} #=> {taro: 3, jiro: 8, saburo: 9} p max_score[:taro] #=> 3 I'm not thinking "apply" is a perfect name for this. Maybe "hash_map" is better (we already have "flat_map"). =end -- http://bugs.ruby-lang.org/