summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-07 12:40:45 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-07 12:40:45 +0000
commit86d05f81fd01c674ace8290e376c026c35b07d2c (patch)
treef143b93ef9a9978593e7aa65e987b370082018a5 /array.c
parent5e644b81b79622a9ebae2d452509a05832b16d08 (diff)
* array.c (rb_ary_nitems): Backport Array#nitems with a block;
suggested by Bertram Scharpf <lists@bertram-scharpf.de> in [ruby-talk:134083]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@15917 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/array.c b/array.c
index c3aeefe..2d345da 100644
--- a/array.c
+++ b/array.c
@@ -2856,11 +2856,16 @@ rb_ary_compact(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
@@ -2868,14 +2873,23 @@ rb_ary_nitems(ary)
VALUE ary;
{
long n = 0;
- VALUE *p, *pend;
+
+ if (rb_block_given_p()) {
+ long i;
- p = RARRAY(ary)->ptr;
- pend = p + RARRAY(ary)->len;
+ 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++;
+ while (p < pend) {
+ if (!NIL_P(*p)) n++;
+ p++;
+ }
}
return LONG2NUM(n);
}