Created
January 21, 2011 01:51
-
-
Save kfaustino/789113 to your computer and use it in GitHub Desktop.
Example: Creating a hash of HTML status codes from an Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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