Skip to content

Instantly share code, notes, and snippets.

@vdw
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdw/939a61dd3fbdb04dc5b8 to your computer and use it in GitHub Desktop.
Save vdw/939a61dd3fbdb04dc5b8 to your computer and use it in GitHub Desktop.
Dotted path to Ruby nested hash

Convert a dotted path to hash

def dotted_path_to_hash(hash)
  hash.map do |pkey, pvalue|
    pkey.to_s.split(".").reverse.inject(pvalue) do |value, key|
      {key.to_sym => value}
    end
  end.inject(&:deep_merge)
end

# { 'level1.level2.level3' => 123 }
# to
# { :level1 => { :level2 => { :level3 => 123 } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment