Method: Hash#smash

Defined in:
lib/inspec/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}



16
17
18
19
20
21
22
23
24
25
# File 'lib/inspec/utils/hash.rb', line 16

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