Issue #6669 has been updated by phluid61 (Matthew Kerwin).


phluid61 (Matthew Kerwin) wrote:
> I might also suggest the name `map_pairs`, derived from `each_pair`.  I
> think it's not incorrect, as the return value from `each_pair` is the
> original Hash object, so the return value from `map_pairs` could also be
> a Hash object.
> 
> Incidentally this paves the way for future possibilities, such as
> `map_keys` or `map_values` (as per `each_key` and `each_value`)

This is related to #7793.  I just released a gem that implements Hash#map_pairs, #map_keys, and #map_values.  https://rubygems.org/gems/hashmap

----------------------------------------
Feature #6669: A method like Hash#map but returns hash
https://bugs.ruby-lang.org/issues/6669#change-37556

Author: yhara (Yutaka HARA)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


=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/