Skip to content

Instantly share code, notes, and snippets.

@kfaustino
Created January 21, 2011 01:51
Show Gist options
  • Save kfaustino/789113 to your computer and use it in GitHub Desktop.
Save kfaustino/789113 to your computer and use it in GitHub Desktop.
Example: Creating a hash of HTML status codes from an Array
status_codes = [['OK',200], ['Created', 201],
['Accepted', 202], ['Moved Permanently', 301],
['Found', 302], ['Not Modified', 304]]
# Create Hash via inject
status_codes.inject({}) do |result, (description, value)|
key = description.downcase.gsub(' ','_')
result[key] = value
result # Explicit return of the hash
end
# Create Hash with each_with_object
status_codes.each_with_object({}) do |(description, value), result|
key = description.downcase.gsub(' ','_')
result[key] = value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment