Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#flat_keys(separator = ".") ⇒ Object



4
5
6
7
8
# File 'lib/flat_keys.rb', line 4

def flat_keys(separator = ".")
  hash = {}
  visit_flat_keys(hash, "", separator)
  return hash
end

#unflat_keys(separator = '.') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flat_keys.rb', line 10

def unflat_keys(separator = '.')
  hash = {}
  self.each do |k, v|
    names = k.split(separator)
    last_index = names.count - 1
    local_hash = hash
    names.each_with_index do |name, index|
      last = index == last_index
      if last
        local_hash[name] = v
      else
        local_hash = local_hash[name] || (local_hash[name] = Hash.new)
      end
    end
  end
  hash
end