Last active
August 29, 2015 13:57
-
-
Save mhenrixon/9483207 to your computer and use it in GitHub Desktop.
How to dotify a ruby hash (to be able to more easily convert it to a java like dot notated config file)
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