Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active December 7, 2025 02:50
Show Gist options
  • Select an option

  • Save epitron/d281c2b03a2a6fcad57b68d2a118880f to your computer and use it in GitHub Desktop.

Select an option

Save epitron/d281c2b03a2a6fcad57b68d2a118880f to your computer and use it in GitHub Desktop.
data = { "a" => %w[x y z], "b" => %w[q r s], "c" => { "d" => %w[g h i] }, "e" => "f", "g" => { "h" => "i" } }
def dotted(thing, prefix: nil)
case thing
when Hash
thing.flat_map do |k, v|
dotted(v, prefix: k).map { |dots| prefix ? "#{prefix}.#{dots}" : dots }
end
when Array
thing.map { |elem| "#{prefix}.#{elem}"}
when String
["#{prefix}.#{thing}"]
else
raise "dunno"
end
end
p dotted(data)
# expected output:
# ["a.x", "a.y", "a.z", "b.q", "b.r", "b.s", "c.d.g", "c.d.h", "c.d.i", "e.f", "g.h.i"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment