public Method

Array.nitems

array.nitems  int

Returns the number of non-nil elements in self. May be zero.

[ 1, nil, 3, nil, 5 ].nitems   #=> 3

Source Code

/*
*  call-seq:
*     array.nitems -> int
*  
*  Returns the number of non-<code>nil</code> elements in _self_.
*  May be zero.
*     
*     [ 1, nil, 3, nil, 5 ].nitems   #=> 3
*/

static VALUE
rb_ary_nitems(ary)
   VALUE ary;
{
   long n = 0;
   VALUE *p, *pend;

   p = RARRAY(ary)->ptr;
   pend = p + RARRAY(ary)->len;

   while (p < pend) {
       if (!NIL_P(*p)) n++;
       p++;
   }
   return LONG2NUM(n);
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.