Skip to content

Instantly share code, notes, and snippets.

@hyfather
Created October 18, 2011 11:53
Show Gist options
  • Save hyfather/1295258 to your computer and use it in GitHub Desktop.
Save hyfather/1295258 to your computer and use it in GitHub Desktop.
[Ruby] Merge all the hashes inside an Array into one big hash
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