Class: Hash
Overview
core_ext/hash.rb – hash extensions
Direct Known Subclasses
Constant Summary collapse
- DEEP_MERGER =
lambda for recursive merges
proc do |key,v1,v2| (v1.respond_to?(:merge) && v2.respond_to?(:merge)) ? v1.merge(v2.compact, &Hash::DEEP_MERGER) : (v2.nil? ? v1 : v2) end
Instance Method Summary collapse
-
#compact ⇒ Object
remove all key-value pairs where the value is nil.
-
#compact! ⇒ Object
Replace the hash with its compacted self.
-
#deep_delete(*args) ⇒ Object
Treat hash as tree of hashes:.
-
#deep_get(*args) ⇒ Object
Treat hash as tree of hashes:.
-
#deep_merge(hsh2) ⇒ Object
Merge hashes recursively.
- #deep_merge!(hsh2) ⇒ Object
-
#deep_set(*args) ⇒ Object
Treat hash as tree of hashes:.
Instance Method Details
#compact ⇒ Object
remove all key-value pairs where the value is nil
98 99 100 |
# File 'lib/configliere/core_ext/hash.rb', line 98 def compact reject{|key,val| val.nil? } end |
#compact! ⇒ Object
Replace the hash with its compacted self
104 105 106 |
# File 'lib/configliere/core_ext/hash.rb', line 104 def compact! replace(compact) end |
#deep_delete(*args) ⇒ Object
Treat hash as tree of hashes:
x = { 1 => :val, :subhash => { 1 => :val1, 2 => :val2 } }
x.deep_delete(:subhash, 1)
#=> :val
x
#=> { 1 => :val, :subhash => { 2 => :val2 } }
89 90 91 92 93 |
# File 'lib/configliere/core_ext/hash.rb', line 89 def deep_delete *args last_key = args.pop last_hsh = args.empty? ? self : (deep_get(*args)||{}) last_hsh.delete(last_key) end |
#deep_get(*args) ⇒ Object
Treat hash as tree of hashes:
x = { 1 => :val, :subhash => { 1 => :val1 } }
x.deep_get(:subhash, 1)
# => :val
x.deep_get(:subhash, 2)
# => nil
x.deep_get(:subhash, 2, 3)
# => nil
x.deep_get(:subhash, 2)
# => nil
71 72 73 74 75 76 77 |
# File 'lib/configliere/core_ext/hash.rb', line 71 def deep_get *args last_key = args.pop # dig down to last subtree (building out if necessary) hsh = args.inject(self){|hsh, key| hsh[key] || {} } # get leaf value hsh[last_key] end |
#deep_merge(hsh2) ⇒ Object
Merge hashes recursively. Nothing special happens to array values
x = { :subhash => { 1 => :val_from_x, 222 => :only_in_x, 333 => :only_in_x }, :scalar => :scalar_from_x}
y = { :subhash => { 1 => :val_from_y, 999 => :only_in_y }, :scalar => :scalar_from_y }
x.deep_merge y
=> {:subhash=>{1=>:val_from_y, 222=>:only_in_x, 333=>:only_in_x, 999=>:only_in_y}, :scalar=>:scalar_from_y}
y.deep_merge x
=> {:subhash=>{1=>:val_from_x, 222=>:only_in_x, 333=>:only_in_x, 999=>:only_in_y}, :scalar=>:scalar_from_x}
Nil values always lose.
x = {:subhash=>{:nil_in_x=>nil, 1=>:val1,}, :nil_in_x=>nil}
y = {:subhash=>{:nil_in_x=>5}, :nil_in_x=>5}
y.deep_merge x
=> {:subhash=>{1=>:val1, :nil_in_x=>5}, :nil_in_x=>5}
x.deep_merge y
=> {:subhash=>{1=>:val1, :nil_in_x=>5}, :nil_in_x=>5}
31 32 33 |
# File 'lib/configliere/core_ext/hash.rb', line 31 def deep_merge hsh2 merge hsh2, &Hash::DEEP_MERGER end |
#deep_merge!(hsh2) ⇒ Object
35 36 37 |
# File 'lib/configliere/core_ext/hash.rb', line 35 def deep_merge! hsh2 merge! hsh2, &Hash::DEEP_MERGER end |
#deep_set(*args) ⇒ Object
Treat hash as tree of hashes:
x = { 1 => :val, :subhash => { 1 => :val1 } }
x.deep_set(:subhash, :cat, :hat)
# => { 1 => :val, :subhash => { 1 => :val1, :cat => :hat } }
x.deep_set(:subhash, 1, :newval)
# => { 1 => :val, :subhash => { 1 => :newval, :cat => :hat } }
49 50 51 52 53 54 55 56 |
# File 'lib/configliere/core_ext/hash.rb', line 49 def deep_set *args val = args.pop last_key = args.pop # dig down to last subtree (building out if necessary) hsh = args.empty? ? self : args.inject(self){|hsh, key| hsh[key] ||= {} } # set leaf value hsh[last_key] = val end |