Method: Weak::Map#merge
- Defined in:
- lib/weak/map.rb
#merge(*other_maps) {|key, old_value, new_value| ... } ⇒ Weak::Map
Note:
Weak::Map does not test member equality with == or eql?.
Instead, it always checks strict object equality, so that, e.g.,
different String keys are not considered equal, even if they may
contain the same content.
Returns the new Weak::Map formed by merging each of other_maps into a
copy of self.
Each argument in other_maps must be either a Weak::Map, a Hash object
or must be transformable to a Hash by calling each_hash on it.
With arguments and no block:
- Returns a new Weak::Map, after the given maps are merged into a copy
of
self. - The given maps are merged left to right.
- Each duplicate-key entry’s value overwrites the previous value.
Example:
map = Weak::Map.new
map[:foo] = 0
map[:bar] = 1
h1 = {baz: 3, bar: 4}
h2 = {bam: 5, baz: 6}
map.merge(h1, h2)
# => #<Weak::Map {:foo=>0, :bar=>4, :baz=>6, :bam=>5}
With arguments and a block:
- Returns
self, after the given maps are merged. - The given maps are merged left to right.
- For each duplicate key:
- Calls the block with the key and the old and new values.
- The block’s return value becomes the new value for the entry.
- The block should only return values which are otherwise strongly referenced to ensure that the value is not immediately garbage-collected.
Example:
map = Weak::Map.new
map[:foo] = 0
map[:bar] = 1
h1 = {baz: 3, bar: 4}
h2 = {bam: 5, baz: 6}
map.merge(h1, h2) { |key, old_value, new_value| old_value + new_value }
# => #<Weak::Map {:foo=>0, :bar=>5, :baz=>9, :bam=>5}
With no arguments:
- Returns a copy of
self. - The block, if given, is ignored.
582 583 584 |
# File 'lib/weak/map.rb', line 582 def merge(*other_maps, &block) dup.merge!(*other_maps, &block) end |