Module: Corefines::Enumerable::MapBy
- Defined in:
- lib/corefines/enumerable.rb
Instance Method Summary collapse
-
#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.
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.
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 |