mirror of
https://github.com/ruby/ruby.git
synced 2025-08-22 20:44:18 +02:00
* enum.c (count_i, count_iter_i): Sync with trunk.
enum.c (enum_count, count_all_i, Init_Enumerable), array.c (rb_ary_count): Sync with trunk. If no argument or block is given, count the number of all elements. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16694 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
019864bfc1
commit
14dd72b35a
6 changed files with 72 additions and 38 deletions
15
array.c
15
array.c
|
@ -3064,27 +3064,34 @@ rb_ary_nitems(ary)
|
|||
|
||||
/*
|
||||
* call-seq:
|
||||
* array.count -> int
|
||||
* array.count(obj) -> int
|
||||
* array.count { |item| block } -> int
|
||||
*
|
||||
* Returns the number of elements which equals to <i>obj</i>.
|
||||
* 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 <i>obj</i>. 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
|
||||
*
|
||||
*/
|
||||
|
||||
static VALUE
|
||||
rb_ary_count(int argc, VALUE *argv, VALUE ary)
|
||||
rb_ary_count(argc, argv, ary)
|
||||
int argc;
|
||||
VALUE *argv;
|
||||
VALUE ary;
|
||||
{
|
||||
long n = 0;
|
||||
|
||||
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++;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue