-
-
Save karnauskas/9483263 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 dotify(hash, k = []) | |
return {k.join('.') => hash} unless hash.is_a?(Hash) | |
hash.inject({}){ |h, v| h.merge! dotify(v[-1], k + [v[0]]) } | |
end | |
describe "dotify" do | |
let(:hash) { { foo: { bar: { baz: true } } } } | |
it "converts hash to dot notated keys" do | |
expect(dotify(hash)).to eq("foo.bar.baz" => true) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment