Method: Hash#smash

Defined in:
lib/utils/hash.rb

#smash(prefix = nil) ⇒ Object

converts a deep hash into a flat hash hash =

'a' => 1,
'b' => {'c' => 2,

} hash.smash # => {“a”=>1, “b-c”=>2}



20
21
22
23
24
25
26
27
28
29
# File 'lib/utils/hash.rb', line 20

def smash(prefix = nil)
  inject({}) do |acc, (key, value)|
    index = prefix.to_s + key.to_s
    if value.is_a?(Hash)
      acc.merge(value.smash(index + '-'))
    else
      acc.merge(index => value)
    end
  end
end