Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Last active August 29, 2015 14:03
Show Gist options
  • Save guilleiguaran/80b68fecc4b69e9b3823 to your computer and use it in GitHub Desktop.
Save guilleiguaran/80b68fecc4b69e9b3823 to your computer and use it in GitHub Desktop.
class Hash
# Allow accessing to hash elements using full key path separated by comma.
#
# hash = {"flash" => {"error" => {"message" => "User type is invalid"}}}
# hash.param("flash.error.message")
# # => "User type is invalid"
#
def param(key)
return self[key] unless key.is_a?(String) && key.include?(".")
key.to_s.split('.').inject(self) do |param, key|
return unless param
param[key]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment