Method: Immutable::Hash#merge
- Defined in:
- lib/immutable/hash.rb
#merge(other) {|key, my_value, other_value| ... } ⇒ Hash
Return a new Hash containing all the key/value pairs from this Hash and other. If no block is provided, the value for entries with colliding keys will be that from other. Otherwise, the value for each duplicate key is determined by calling the block.
other can be an Immutable::Hash, a built-in Ruby Hash, or any Enumerable object which yields ‘[key, value]` pairs.
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/immutable/hash.rb', line 485 def merge(other) trie = if block_given? other.reduce(@trie) do |trie, (key, value)| if entry = trie.get(key) trie.put(key, yield(key, entry[1], value)) else trie.put(key, value) end end else @trie.bulk_put(other) end derive_new_hash(trie) end |