Skip to content

Instantly share code, notes, and snippets.

@karnauskas
Forked from mhenrixon/dotify_spec.rb
Created March 11, 2014 10:38
Show Gist options
  • Save karnauskas/9483263 to your computer and use it in GitHub Desktop.
Save karnauskas/9483263 to your computer and use it in GitHub Desktop.
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