Skip to content

Instantly share code, notes, and snippets.

@masaomoc
Last active April 14, 2017 01:53
Show Gist options
  • Save masaomoc/1a58108424399f314142123a63807f8f to your computer and use it in GitHub Desktop.
Save masaomoc/1a58108424399f314142123a63807f8f to your computer and use it in GitHub Desktop.
def make_parent_path(variables, prefix='')
h = {}
variables.each do |k,v|
key = "#{prefix}#{k.to_s}"
if v.is_a? Hash
h.merge!(make_parent_path(v, "#{key}."))
else
h[key] = v
end
end
h
end
h = {
a: 1,
b: {
c: 2,
d: 3
}
}
make_parent_hash(h)
# {
# "a" => 1,
# "b.c" => 2,
# "b.d" => 3
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment