Last Updated: February 25, 2016
·
238
· dpaluy

Enumerable Histogram

module Enumerable

  def to_histogram
    inject(Hash.new(0)) { |h, x| h[x] += 1; h}
  end

end
%w(a b c d a a b).to_histogram

will return:
{
"a" => 3,
"b" => 2,
"c" => 1,
"d" => 1
}