Skip to content

Instantly share code, notes, and snippets.

@bogdanovich
Last active June 27, 2016 18:56
Show Gist options
  • Save bogdanovich/f5145e52620e1d3e724e7be6b21cb0b3 to your computer and use it in GitHub Desktop.
Save bogdanovich/f5145e52620e1d3e724e7be6b21cb0b3 to your computer and use it in GitHub Desktop.
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