Count the number of occurrences
Since version 2.7, the Enumerable
module has a tally
method that returns a Hash
with the count of each element.
And since version 3.1, it accepts an optional Hash
as a parameter, which it returns, to accumulate multiple counts.
The origin of the request was directly for tally_by
(https://bugs.ruby-lang.org/issues/11076). In the meantime, you can always use the « old » way!
Link to Ruby documentation for tally
.