Created
October 18, 2011 11:53
-
-
Save hyfather/1295258 to your computer and use it in GitHub Desktop.
[Ruby] Merge all the hashes inside an Array into one big hash
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
class Array | |
def flatten_hashes | |
Hash[*self.map(&:to_a).flatten] | |
end | |
end | |
[{:a => 'alpha'}, {:b => 'beta'}, {:c => 'charlie'}].flatten_hashes | |
#=> {:a => 'alpha', :b => 'beta', :c => 'charlie'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment