Sha256: eb26d00879aa51ffa6cdf1f0a46d1c866ee4a98baf900a55a18eba6bf904d2b3
Contents?: true
Size: 571 Bytes
Versions: 19
Compression:
Stored size: 571 Bytes
Contents
module MoreCoreExtensions module ArrayElementCounts # Returns a Hash of each element to the count of those elements. Optionally # pass a block to count by a different criteria. # # [1, 2, 3, 1, 3, 1].counts # => {1 => 3, 2 => 1, 3 => 2} # %w(a aa aaa a aaa a).counts { |i| i.length } # => {1 => 3, 2 => 1, 3 => 2} # def element_counts each_with_object(Hash.new(0)) do |i, h| key = block_given? ? yield(i) : i h[key] += 1 end end end end Array.send(:include, MoreCoreExtensions::ArrayElementCounts)
Version data entries
19 entries across 19 versions & 1 rubygems