Skip to content

Instantly share code, notes, and snippets.

@thomasklemm
Created September 15, 2012 17:07
Show Gist options
  • Save thomasklemm/3728865 to your computer and use it in GitHub Desktop.
Save thomasklemm/3728865 to your computer and use it in GitHub Desktop.
Array of arrays to hash conversion
##
# Array of arrays to hash conversion
#
##
# Version 1
array = [[1, 1], [2, 2], [3, 3]]
hash = Hash[*array.flatten]
p hash
# => {1=>1, 2=>2, 3=>3}
##
# Version 2
class Array
def to_hash
Hash[*self.flatten]
end
end
array = [[1, 1], [2, 2], [3, 3]]
p array.to_hash
# => {1=>1, 2=>2, 3=>3}
http_header_rules = [
[:global, {'Cache-Control' =>'public, max-age=42'}],
[:fonts, {'Access-Control' => '*'}]
]
p http_header_rules.to_hash
# => {:global=>{"Cache-Control"=>"public, max-age=42"}, :fonts=>{"Access-Control"=>"*"}}
@thomasklemm
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment