Module: Corefines::Enumerable::MapBy

Defined in:
lib/corefines/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#map_by {|obj| ... } ⇒ Hash

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.

Examples:

a = [1, 2, 3, 4, 5]
a.map_by { |e| [e % 2, e + 1] }
#=> { 0 => [3, 5], 1 => [2, 4, 6] }
h = { "Lucy" => 86, "Ruby" => 98, "Drew" => 94, "Lisa" => 54 }
h.map_by { |k, v| [score(v), k] }
#=> { "A" => ["Ruby", "Drew"], "B" => ["Lucy"], "F" => ["Lisa"] }

Yields:

  • (obj)

    gives each element to the block.

Yield Returns:

  • (Array)

    an array with the key and the value.

Returns:


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/corefines/enumerable.rb', line 103

module MapBy
  Support.classes_including_module(::Enumerable) do |klass|

    refine klass do
      def map_by
        res = {}
        each do |e|
          k, v = yield(*e)
          (res[k] ||= []) << v
        end
        res
      end
    end
  end
end