Class: Hash

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

Direct Known Subclasses

Aduki::RecursiveHash

Instance Method Summary collapse

Instance Method Details

#recursively_delete_keys!(*keys_to_delete) ⇒ Object



2
3
4
5
6
# File 'lib/core_ext/hash.rb', line 2

def recursively_delete_keys! *keys_to_delete
  keys_to_delete.each { |k| delete k }
  values.each { |v| v.recursively_delete_keys!(*keys_to_delete) if v.is_a?(Hash) || v.is_a?(Array) }
  self
end

#recursively_remove_by_value!(&block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/core_ext/hash.rb', line 24

def recursively_remove_by_value! &block
  self.delete_if { |k, v|
    if v.is_a? Hash
      v.recursively_remove_by_value! &block
      v.empty?
    elsif v.is_a? Array
      v.recursively_remove_by_value! &block
      v.empty?
    else
      yield v
    end
  }
end

#recursively_replace_keys!(&block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/core_ext/hash.rb', line 8

def recursively_replace_keys! &block
  keys.each do |k|
    v = self[k]
    mk = yield k
    if k != mk
      self[mk] = delete k
    end
    v.recursively_replace_keys!(&block) if v.is_a?(Hash) || v.is_a?(Array)
  end
  self
end

#recursively_replace_values(&block) ⇒ Object



38
39
40
41
42
# File 'lib/core_ext/hash.rb', line 38

def recursively_replace_values &block
  each_with_object({ }) { |(k, v), result|
    result[k] = (v.is_a?(Hash) || v.is_a?(Array)) ? v.recursively_replace_values(&block) : yield(k, v)
  }
end

#recursively_stringify_keys!Object



20
21
22
# File 'lib/core_ext/hash.rb', line 20

def recursively_stringify_keys!
  recursively_replace_keys! { |k| k.to_s }
end