Skip to content

Instantly share code, notes, and snippets.

@bnorton
Created June 11, 2014 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnorton/823f648082e48aa1a860 to your computer and use it in GitHub Desktop.
Save bnorton/823f648082e48aa1a860 to your computer and use it in GitHub Desktop.
Aggregate records in memory by
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