Add tally method for older rubies

This commit is contained in:
kojix2 2020-08-03 10:44:49 +09:00
parent d069f8af23
commit 78559b989e

View File

@ -205,3 +205,14 @@ module Uplot
end end
end end
end end
# backports
# https://github.com/marcandre/backports/blob/master/lib/backports/2.7.0/enumerable/tally.rb
unless Enumerable.method_defined? :tally
module Enumerable
def tally
# NB: By spec, tally should return default-less hash
each_with_object(Hash.new(0)) { |item, res| res[item] += 1 }.tap { |h| h.default = nil }
end
end
end