Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/keboola/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#reverse_merge(other_hash) ⇒ Object

Merges the caller into other_hash. For example,

options = options.reverse_merge(size: 25, velocity: 10)

is equivalent to

options = { size: 25, velocity: 10 }.merge(options)

This is particularly useful for initializing an options hash with default values.


12
13
14
# File 'lib/keboola/core_ext/hash.rb', line 12

def reverse_merge(other_hash)
  other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object Also known as: reverse_update

Destructive reverse_merge.


17
18
19
20
# File 'lib/keboola/core_ext/hash.rb', line 17

def reverse_merge!(other_hash)
  # right wins if there is no left
  merge!( other_hash ){|key,left,right| left }
end