Last active
December 7, 2025 02:50
-
-
Save epitron/d281c2b03a2a6fcad57b68d2a118880f to your computer and use it in GitHub Desktop.
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
| 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