Skip to content

Instantly share code, notes, and snippets.

@vdw
Last active January 19, 2018 10:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vdw/f3c832df8ce271a036f2 to your computer and use it in GitHub Desktop.
Save vdw/f3c832df8ce271a036f2 to your computer and use it in GitHub Desktop.
Ruby hash to dotted path

Convert a ruby hash to dotted path

def hash_to_dotted_path(hash, path = "")
  hash.each_with_object({}) do |(k, v), ret|
    key = path + k.to_s

    if v.is_a? Hash
      ret.merge! hash_to_dotted_path(v, key.to_s + ".")
    else
      ret[key] = v
    end
  end
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