Skip to content

Instantly share code, notes, and snippets.

@henrygarner
Created March 30, 2012 09:00
Show Gist options
  • Save henrygarner/2250089 to your computer and use it in GitHub Desktop.
Save henrygarner/2250089 to your computer and use it in GitHub Desktop.
Recursively flatten nested Hashes
class Hash
def flatten_nested(hash = {}, prefix = '')
each do |key, value|
full_key = prefix.empty? ? key : "#{prefix}_#{key}"
value.is_a?(Hash) ? value.flatten_nested(hash, full_key) : hash[full_key] = value
end
hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment