Ruby 2.7 — Enumerable#tally

Christmas has come and passed, 2.6 has been released, and now it’s time to mercilessly hawk the releases page for 2.7 features so we can start our fun little annual tradition of blogging about upcoming features.

Typically this means another December release, but there have been cases of methods making it in earlier if they’re merged to trunk this early in the year.

This round? We have the new method Enumerable#tally!

The Short Version

[1, 1, 2].tally
# => { 1 => 2, 2 => 1 }
[1, 1, 2].map(&:even?).tally
# => { false => 2, true => 1 }


[1, 2, 2, 3].tally
# => { 1 => 1, 2 => 2, 3 => 1 }

Without a block, tally works by counting the occurrences of each element in an Enumerable type. If we apply that to a list of another type it might be a bit clearer:

%w(foo foo bar foo baz foo).tally
=> {"foo"=>4, "bar"=>1, "baz"=>1}

Currently tally_by has not been accepted into core, so to tally by a function you would instead use map first:

%w(foo foo bar foo baz foo).map { |s| s[0] }.tally
=> {“f” => 4, “b” => 2}

There’s discussion happening at the moment on accepting this feature, which would make the above syntax:

%w(foo foo bar foo baz foo).tally_by { |s| s[0] }
=> {“f” => 4, “b” => 2}

Why Use It?

list.group_by { |v| v.something }.transform_values(&:size)list.group_by { |v| v.something }.map { |k, vs| [k, vs.size] }.to_hlist.group_by { |v| v.something }.to_h { |k, vs| [k, vs.size] }list.each_with_object( { |v, h| h[v.something] += 1 }

There are likely several more variants of this, but those are a few of the more common ones you might see around. This is a nicety method to abbreviate a very common idiom in the Ruby language, and a very welcome one.

Vanilla Ruby Equivalent

module Enumerable
def tally_by(&function)
function ||= -> v { v }

each_with_object( do |value, hash|
hash[] += 1

def tally

In the case of no provided function, it would effectively be tallying by itself, or rather an identity function.

An identity function is a function that returns what it was given. If you give it 1, it returns 1. If you give it true, it returns true. Ruby also uses this concept in a method called itself.

This article will not go into great depth on what the above code does. Part Five of “Reducing Enumerable” covers this code in more detail:

The Source Code

It was accepted by the Ruby core team under the name tally :


A tally is a record of amounts or numbers which you keep changing and adding to as the activity which affects it progresses.


Now where did this name come from? Originally the name count_by was proposed, but the name was rejected as it differed from count which has a different return type and behavior.

On a car ride back from the Tahoe area and RailsCamp West we (myself, David, Stephanie, and Shannon) were discussing potentially alternate names to try and propose to see if the feature could get in under a different name.

David had proposed tally, and formally suggested it. It looks like the name stuck, and the code’s been merged into trunk.

Now I’d presented a talk at a few conferences, and decided to mention tally_by instead of count_by in my RubyConf talk in one section. The written version is over here:

Just a bit of interesting backstory.

Wrapping Up

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store