Class: Hash

Inherits:
Object show all
Defined in:
lib/slimkeyfy/slimutils/hash_merging.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autonew(*args) ⇒ Object



70
71
72
73
# File 'lib/slimkeyfy/slimutils/hash_merging.rb', line 70

def self.autonew(*args)
  leet = lambda { |hsh, key| hsh[key] = new( &leet ) }
  new(*args,&leet)
end

Instance Method Details

#deep_merge(second) ⇒ Object



62
63
64
65
# File 'lib/slimkeyfy/slimutils/hash_merging.rb', line 62

def deep_merge(second)
  merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
  self.merge(second, &merger)
end

#explode(divider = '.') ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/slimkeyfy/slimutils/hash_merging.rb', line 75

def explode(divider = '.')
  h = Hash.autonew
  for k,v in self.dup
    tree = k.split(divider).map { |x| [ :[], x ] }
    tree.push([ :[]=, tree.pop[1], v ])
    h.recursive_send(*tree)
  end
  h
end