Sha256: 50f04102ba86bd9d3c7b9cfc5961f129a1013fd186207b3633547ed40b487c91

Contents?: true

Size: 820 Bytes

Versions: 5

Compression:

Stored size: 820 Bytes

Contents

require 'facets/hash/count'

module Enumerable

  unless method_defined?(:count)

    # Count the number of items in an enumerable
    # equal (==) to the given object(s).
    #
    #   e = [ 'a', '1', 'a' ]
    #   e.count('1')     #=> 1
    #   e.count('a')     #=> 2
    #   e.count('a', 1)  #=> 3
    #
    # Note that Hash#count only considers values.
    #
    #   e = { 'a' => 2, 'x' => 2, 'b' => 1 }
    #   e.count(1) #=> 1
    #   e.count(2) #=> 2
    #
    # CREDIT: Trans

    def count(*items, &block)
      if block || !items.empty?
        r = self
        r = r.select(&block) if block
        r = r.select{ |x| items.any?{ |i| i == x } } if !items.empty?
        r.size
      else
        begin
          size
        rescue
          i=0; each{ |e| i+=1 }; i
        end
      end
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/enumerable/count.rb
facets-2.8.3 lib/core/facets/enumerable/count.rb
facets-2.8.2 lib/core/facets/enumerable/count.rb
facets-2.8.1 lib/core/facets/enumerable/count.rb
facets-2.8.0 lib/core/facets/enumerable/count.rb