Last active
June 27, 2016 18:56
-
-
Save bogdanovich/f5145e52620e1d3e724e7be6b21cb0b3 to your computer and use it in GitHub Desktop.
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
def flatten(dict) | |
output_dict = {} | |
dict.each do |k,v| | |
if v.is_a? Hash | |
sub_dict = flatten(v) | |
sub_dict.each do |sk, sv| | |
output_dict[k+'.'+sk] = sv | |
end | |
else | |
output_dict[k] = v | |
end | |
end | |
output_dict | |
end | |
# input = { | |
# 'Key1' => '1', | |
# 'Key2' => { | |
# 'a' => '2', | |
# 'b' => '3', | |
# 'c' => { | |
# 'd' => '3', | |
# 'e' => '1' | |
# } | |
# } | |
# } | |
# output = { | |
# 'Key1' => '1', | |
# 'Key2.a' => '2', | |
# 'Key2.b' => '3', | |
# 'Key2.c.d' => '3', | |
# 'Key2.c.e' => '1' | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment