Posted 8 months ago. Visible to the public.

Custom Ruby method Enumerable#count_by (use for quick statistics)

I frequently find myself needing a combination of group_by, count and sort for quick statistics. Here's a method on Enumerable that combines the three:

module Enumerable def count_by(&block) list = group_by(&block) .map{ |key, items| [key, items.count] } .sort_by(&:last) Hash[list] end end

The new method returns a Hash of key => count pairs.

Usage examples

Number of email addresses by domain:

> User.all.count_by { |user| /^.*@/, '' } => { ""=>2, ..., ""=>63, ""=>128, ""=>153}


  • Number of new users per day: User.all.count_by { |user| user.created_at.to_date }
  • Number of articles per brand: Article.all.count_by &:brand

Note that the last simple example can also be achieved with Rails internals: This translates to SQL, so it executes fast. However, grouping is restricted to columns (attributes). Using #count_by gives you the full flexibility of Ruby.

More tools

If you need further options, here's a "toolbox" of chainable method invocations:

list .group_by{ |item| } # Group .map{ |key, items| [key, items.count] } # Count .select{ |key, count| count > 10 } # Filter .sort_by(&:last) # Sort ASC .sort_by{ |key, count| -count } # Sort DESC .each{ |key, count| puts "#{count.to_s.rjust(6)} #{key}" } # Print

Wrap the result in Hash[...] to turn a list-of-pairs into a Hash.

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Author of this card:

Dominik Schöler
Last edit:
8 months ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev