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
}
Written by David Paluy
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#