summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--NEWS4
-rw-r--r--array.c26
3 files changed, 30 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index aa1c41a328..fa2db35eee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Mon Apr 7 21:35:08 2008 Akinori MUSHA <knu@iDaemons.org>
+
+ * array.c (rb_ary_nitems): Backport Array#nitems with a block;
+ suggested by Bertram Scharpf <lists@bertram-scharpf.de> in
+ [ruby-talk:134083].
+
Sun Apr 6 09:45:00 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* dir.c (dir_tell): check if closed. [ruby-core:16223]
diff --git a/NEWS b/NEWS
index 74b552c6f2..82f327030f 100644
--- a/NEWS
+++ b/NEWS
@@ -17,6 +17,10 @@ with all sufficient information, see the ChangeLog file.
* builtin classes
+ * Array#nitems now takes a block optionally, which is used to
+ determine if each element should be counted instead of checking if
+ the element is non-nil.
+
* Integer#ord implemented.
* Integer#odd? implemented.
* Integer#even? implemented.
diff --git a/array.c b/array.c
index c3aeefe372..2d345dab5d 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);
}