latest / Enumerable#tally beats group_by(&:itself).transform_values(&:count)
I keep reaching for group_by when I just want to count occurrences.
words = %w[a b a c b a]
words.group_by(&:itself).transform_values(&:count)
# => {"a"=>3, "b"=>2, "c"=>1}
words.tally
# => {"a"=>3, "b"=>2, "c"=>1}
tally has been around since Ruby 2.7. There’s also tally_by in 3.1 if the grouping key isn’t the element itself.
%w[apple banana cherry].tally_by(&:length)
# => {5=>2, 6=>1}