Created
April 10, 2015 13:49
-
-
Save panbanda/815bbbe2ad9e35b613e6 to your computer and use it in GitHub Desktop.
Flatten Hash Ruby on Rails
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
def flatten_hash hash | |
hash.each_with_object({}) do |(k, v), h| | |
if v.is_a? Hash | |
flatten_hash(v).map do |h_k, h_v| | |
h["#{h_k}"] = h_v | |
end | |
else | |
h[k] = v | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment