This is a multi-part message in MIME format. --------------070703040305010404040506 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit I've attached a patch to alter Enumerable#count and Array#count so that when invoked with no argument and no block they behave like the Array#nitems method of Ruby 1.8: they count non-nil items instead of returning an Enumerator. David --------------070703040305010404040506 Content-Type: text/plain; name atch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename atch" Index: array.c --- array.c (revision 16676) +++ array.c (working copy) @@ -2749,9 +2749,12 @@ * call-seq: * array.count(obj) -> int * array.count { |item| block } -> int + * array.count -> int * * Returns the number of elements which equals to <i>obj</i>. - * If a block is given, counts tthe number of elements yielding a true value. + * If a block is given, counts the number of elements yielding a true value. + * If no argument or block is given, returns the number of non-nil items + * (false is considered non-nil). * * ary 1, 2, 4, 2] * ary.count(2) # 2 @@ -2767,11 +2770,16 @@ if (argc 0) { VALUE *p, *pend; - RETURN_ENUMERATOR(ary, 0, 0); - - for (p ARRAY_PTR(ary), pend + RARRAY_LEN(ary); p < pend; p++) { - if (RTEST(rb_yield(*p))) n++; + if (rb_block_given_p()) { + for (p ARRAY_PTR(ary), pend +RARRAY_LEN(ary); p<pend; p++) { + if (RTEST(rb_yield(*p))) n++; + } } + else { + for (p ARRAY_PTR(ary), pend +RARRAY_LEN(ary); p<pend; p++) { + if (!NIL_P(*p)) n++; + } + } } else { VALUE obj, *p, *pend; Index: enum.c --- enum.c (revision 16676) +++ enum.c (working copy) @@ -98,6 +98,19 @@ } static VALUE +count_nonnil_i(VALUE i, VALUE memop, int argc, VALUE *argv) +{ + VALUE *memo VALUE*)memop; + + ENUM_WANT_SVALUE(); + + if (!NIL_P(i)) { + memo[0]++; + } + return Qnil; +} + +static VALUE count_iter_i(VALUE i, VALUE memop, int argc, VALUE *argv) { VALUE *memo VALUE*)memop; @@ -112,9 +125,12 @@ * call-seq: * enum.count(item) int * enum.count {| obj | block } int + * enum.count int * * Returns the number of items in <i>enum</i> for which equals to <i>item</i>. * If a block is given, counts the number of elements yielding a true value. + * If no argument or block is given, returns the number of non-nil items. + * (False is considered non-nil.) * * ary 1, 2, 4, 2] * ary.count(2) # 2 @@ -129,8 +145,12 @@ rb_block_call_func *func; if (argc 0) { - RETURN_ENUMERATOR(obj, 0, 0); - func ount_iter_i; + if (rb_block_given_p()) { + func ount_iter_i; + } + else { + func ount_nonnil_i; + } } else { rb_scan_args(argc, argv, "1", &memo[1]); --------------070703040305010404040506--