module Enumerable def to_histogram inject(Hash.new(0)) { |h, x| h[x] += 1; h} endendp [1, 2, 2, 2, 3, 3].to_histogram# => {1=>1, 2=>3, 3=>2}p ["a", "b", nil, "c", "b", nil, "a"].to_histogram# => {"a"=>2, "b"=>2, "c"=>1, nil=>2}