Created
March 30, 2012 09:00
-
-
Save henrygarner/2250089 to your computer and use it in GitHub Desktop.
Recursively flatten nested Hashes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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