Module: Corefines::Hash::FlatMap
- Defined in:
- lib/corefines/hash.rb
Instance Method Summary collapse
-
#flat_map {|key, value| ... } ⇒ Hash
Returns a new hash with the merged results of running the block once for every entry in
self.
Instance Method Details
#flat_map {|key, value| ... } ⇒ Hash
Returns a new hash with the merged results of running the block once
for every entry in self.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/corefines/hash.rb', line 88 module FlatMap refine ::Hash do def flat_map each_with_object({}) do |(key, value), hash| yielded = yield(key, value) if yielded.is_a? ::Hash hash.merge!(yielded) elsif yielded.is_a? ::Array # Array#to_h exists since Ruby 2.1. yielded.each do |pair| hash.store(*pair) end elsif yielded hash.merge!(yielded.to_h) end end end end end |