Created
September 15, 2012 17:07
-
-
Save thomasklemm/3728865 to your computer and use it in GitHub Desktop.
Array of arrays to hash conversion
This file contains hidden or 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
## | |
# 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"=>"*"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Via: Ruby Pearls vol. 1 - The Splat