Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/hash_ext.rb,
lib/aspera/hash_ext.rb,
lib/aspera/hash_ext.rb,
lib/aspera/hash_ext.rb

Instance Method Summary collapse

Instance Method Details

#deep_do(memory = nil, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/aspera/hash_ext.rb', line 12

def deep_do(memory=nil, &block)
  each do |key, value|
    if value.is_a?(Hash)
      value.deep_do(memory, &block)
    else
      yield(self, key, value, memory)
    end
  end
end

#deep_merge(second) ⇒ Object



4
5
6
# File 'lib/aspera/hash_ext.rb', line 4

def deep_merge(second)
  merge(second){|_key, v1, v2|v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.deep_merge(v2) : v2}
end

#deep_merge!(second) ⇒ Object



8
9
10
# File 'lib/aspera/hash_ext.rb', line 8

def deep_merge!(second)
  merge!(second){|_key, v1, v2|v1.is_a?(Hash) && v2.is_a?(Hash) ? v1.deep_merge!(v2) : v2}
end

#stringify_keysObject



45
46
47
# File 'lib/aspera/hash_ext.rb', line 45

def stringify_keys
  return transform_keys(&:to_s)
end

#symbolize_keysObject



36
37
38
# File 'lib/aspera/hash_ext.rb', line 36

def symbolize_keys
  return transform_keys(&:to_sym)
end

#transform_keysObject



26
27
28
29
# File 'lib/aspera/hash_ext.rb', line 26

def transform_keys
  raise 'missing block' unless block_given?
  return each_with_object({}){|(k, v), memo|memo[yield(k)] = v}
end