Module: Yacht::HashHelper

Defined in:
lib/yacht/hash_helper.rb

Class Method Summary collapse

Class Method Details

.deep_merge(some_hash, other_hash) ⇒ Object

adapted from github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/deep_merge.rb Returns a new hash with some_hash and other_hash merged recursively. Equivalent to some_hash.deep_merge(other_hash) in Rails



5
6
7
8
9
10
11
# File 'lib/yacht/hash_helper.rb', line 5

def deep_merge(some_hash, other_hash)
  some_hash.merge(other_hash) do |key, oldval, newval|
    oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
    newval = newval.to_hash if newval.respond_to?(:to_hash)
    oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? deep_merge(oldval, newval) : newval
  end
end

.slice(some_hash, *keys) ⇒ Object

adapted from as.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Slice.html Returns a new hash with only the given keys



16
17
18
19
20
21
# File 'lib/yacht/hash_helper.rb', line 16

def slice(some_hash, *keys)
  allowed = Set.new(keys)
  hash = {}
  allowed.each { |k| hash[k] = some_hash[k] if some_hash.has_key?(k) }
  hash
end