Created
June 11, 2014 04:31
-
-
Save bnorton/823f648082e48aa1a860 to your computer and use it in GitHub Desktop.
Aggregate records in memory by
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
puts records.inspect | |
# #=> [ | |
# {"id": 1, "created_at": 2014-06-04 06:07:31 -0700}, | |
# {"id": 2, "created_at": 2014-06-07 09:03:11 -0700}, | |
# {"id": 3, "created_at": 2014-06-09 07:47:18 -0700} | |
# ] | |
records.each_with_object(Hash.new { 0 }) do |record, hash| | |
hash[record['created_at'].to_date.beginning_of_week] += 1 | |
end | |
#=> { Mon, 02 Jun 2014 => 2, Mon, 09 Jun 2014 => 1 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment