Skip to content

Instantly share code, notes, and snippets.

@mhenrixon
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mhenrixon/9483207 to your computer and use it in GitHub Desktop.
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)
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