* array.c (rb_ary_nitems, Init_Array): Axe Array#nitems().

cf. [ruby-dev:34676]-[ruby-dev:34713]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16675 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2008-05-28 09:12:28 +00:00
parent e2db8af8a3
commit 8f706b2d44
4 changed files with 7 additions and 54 deletions

41
array.c
View file

@ -2745,46 +2745,6 @@ rb_ary_compact(VALUE ary)
return ary;
}
/*
* call-seq:
* array.nitems -> int
* array.nitems { |item| block } -> int
*
* Returns the number of non-<code>nil</code> elements in _self_.
* If a block is given, the elements yielding a true value are
* counted.
*
* May be zero.
*
* [ 1, nil, 3, nil, 5 ].nitems #=> 3
* [5,6,7,8,9].nitems { |x| x % 2 != 0 } #=> 3
*/
static VALUE
rb_ary_nitems(VALUE ary)
{
long n = 0;
if (rb_block_given_p()) {
long i;
for (i=0; i<RARRAY_LEN(ary); i++) {
VALUE v = RARRAY_PTR(ary)[i];
if (RTEST(rb_yield(v))) n++;
}
}
else {
VALUE *p = RARRAY_PTR(ary);
VALUE *pend = p + RARRAY_LEN(ary);
while (p < pend) {
if (!NIL_P(*p)) n++;
p++;
}
}
return LONG2NUM(n);
}
/*
* call-seq:
* array.count(obj) -> int
@ -3519,7 +3479,6 @@ Init_Array(void)
rb_define_method(rb_cArray, "compact!", rb_ary_compact_bang, 0);
rb_define_method(rb_cArray, "flatten", rb_ary_flatten, -1);
rb_define_method(rb_cArray, "flatten!", rb_ary_flatten_bang, -1);
rb_define_method(rb_cArray, "nitems", rb_ary_nitems, 0);
rb_define_method(rb_cArray, "count", rb_ary_count, -1);
rb_define_method(rb_cArray, "shuffle!", rb_ary_shuffle_bang, 0);
rb_define_method(rb_cArray, "shuffle", rb_ary_shuffle, 0);