From 3a3e250975d0ce34b0e5628a2fc2a3bfa7722188 Mon Sep 17 00:00:00 2001 From: knu Date: Thu, 29 May 2008 19:18:54 +0000 Subject: * enum.c (enum_count, count_all_i, Init_Enumerable), array.c (rb_ary_count): If no argument or block is given, count the number of all elements. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16693 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- array.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'array.c') diff --git a/array.c b/array.c index d452eade3a..787c27b983 100644 --- a/array.c +++ b/array.c @@ -2747,13 +2747,16 @@ rb_ary_compact(VALUE ary) /* * call-seq: + * array.count -> int * array.count(obj) -> int * array.count { |item| block } -> int * - * Returns the number of elements which equals to obj. - * If a block is given, counts the number of elements yielding a true value. + * Returns the number of elements. If an argument is given, counts + * the number of elements which equals to obj. If a block is + * given, counts the number of elements yielding a true value. * * ary = [1, 2, 4, 2] + * ary.count # => 4 * ary.count(2) # => 2 * ary.count{|x|x%2==0} # => 3 * @@ -2767,7 +2770,8 @@ rb_ary_count(int argc, VALUE *argv, VALUE ary) if (argc == 0) { VALUE *p, *pend; - RETURN_ENUMERATOR(ary, 0, 0); + if (!rb_block_given_p()) + return LONG2NUM(RARRAY_LEN(ary)); for (p = RARRAY_PTR(ary), pend = p + RARRAY_LEN(ary); p < pend; p++) { if (RTEST(rb_yield(*p))) n++; -- cgit v1.2.3