Created
May 14, 2013 16:58
Hash unnest method
This file contains 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 unnest | |
new_hash = {} | |
each do |key,val| | |
if val.is_a?(Hash) | |
new_hash.merge!(val.prefix_keys("#{key}.")) | |
else | |
new_hash[key] = val | |
end | |
end | |
new_hash | |
end | |
def prefix_keys(prefix) | |
Hash[map{|key,val| [prefix + key, val]}].unnest | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment