Sha256: 250e7bdebeca9c1f6448b79e206d48f96b5d07d6d0ab9b91c2d579433b91727f

Contents?: true

Size: 938 Bytes

Versions: 16

Compression:

Stored size: 938 Bytes

Contents

module Enumerable

  # Count the number of items in an enuerable equal (==)
  # to the given object.
  #
  #   e = [ 'a', '1', 'a' ]
  #   e.count('1')    #=> 1
  #   e.count('a')    #=> 2
  #
  # Count can also handle multiple-valued blocks.
  #
  #   e = { 'a' => 2, 'a' => 2, 'b' => 1 }
  #   e.count('a',2)  #=> 1
  #
  def count(*c)
    self.select{ |*i| i == c }.length
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TC01 < Test::Unit::TestCase

    def test_001
      e = [ 'a', '1', 'a' ]
      assert_equal( 1, e.count('1') )
      assert_equal( 2, e.count('a') )
    end

    def test_002
      e = [ ['a',2], ['a',2], ['a',2], ['b',1] ]
      assert_equal( 3, e.count(['a',2]) )
    end

    def test_003
      e = { 'a' => 2, 'a' => 2, 'b' => 1 }
      assert_equal( 1, e.count('a',2) )
    end

  end

=end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
facets-1.4.0 lib/facets/core/enumerable/count.rb
facets-1.4.1 lib/facets/core/enumerable/count.rb
facets-1.4.2 lib/facets/core/enumerable/count.rb
facets-1.4.3 lib/facets/core/enumerable/count.rb
facets-1.4.4 lib/facets/core/enumerable/count.rb
facets-1.4.5 lib/facets/core/enumerable/count.rb
facets-1.7.0 lib/facets/core/enumerable/count.rb
facets-1.7.30 lib/facets/core/enumerable/count.rb
facets-1.7.38 lib/facets/core/enumerable/count.rb
facets-1.7.46 lib/facets/core/enumerable/count.rb
facets-1.8.0 lib/facets/core/enumerable/count.rb
facets-1.8.20 lib/facets/core/enumerable/count.rb
facets-1.8.49 lib/facets/core/enumerable/count.rb
facets-1.8.51 lib/facets/core/enumerable/count.rb
facets-1.8.54 lib/facets/core/enumerable/count.rb
facets-1.8.8 lib/facets/core/enumerable/count.rb