module Corefines::Enumerable::MapBy

@!method #map_by(&block)

Converts enumerable into a Hash, iterating over each element where the
provided block must return an array of two elements: a key and a value
to be added into an array that corresponds to that key.

It's similar to {::Enumerable#group_by}, but allows to map the value
being added into a group.

@example
  a = [1, 2, 3, 4, 5]
  a.map_by { |e| [e % 2, e + 1] }
  #=> { 0 => [3, 5], 1 => [2, 4, 6] }

@example
  h = { "Lucy" => 86, "Ruby" => 98, "Drew" => 94, "Lisa" => 54 }
  h.map_by { |k, v| [score(v), k] }
  #=> { "A" => ["Ruby", "Drew"], "B" => ["Lucy"], "F" => ["Lisa"] }

@yield [obj] gives each element to the block.
@yieldreturn [Array] an array with the key and the value.
@return [Hash]

Public Instance Methods

map_by() { |*e| ... } click to toggle source
# File lib/corefines/enumerable.rb, line 107
def map_by
  res = {}
  each do |e|
    k, v = yield(*e)
    (res[k] ||= []) << v
  end
  res
end