public Method

Array.last(...)

array.last       obj or nil
array.last(n)    an_array

Returns the last element(s) of self. If the array is empty, the first form returns nil.

[ "w", "x", "y", "z" ].last   #=> "z"

Source Code

/*
*  call-seq:
*     array.last     ->  obj or nil
*     array.last(n)  ->  an_array
*  
*  Returns the last element(s) of <i>self</i>. If the array is empty,
*  the first form returns <code>nil</code>.
*     
*     [ "w", "x", "y", "z" ].last   #=> "z"
*/

static VALUE
rb_ary_last(argc, argv, ary)
   int argc;
   VALUE *argv;
   VALUE ary;
{
   if (argc == 0) {
       if (RARRAY(ary)->len == 0) return Qnil;
       return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
   }
   else {
       VALUE nv, result;
       long n, i;

       rb_scan_args(argc, argv, "01", &nv);
       n = NUM2LONG(nv);
       if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
       result = rb_ary_new2(n);
       for (i=RARRAY(ary)->len-n; n--; i++) {
           rb_ary_push(result, RARRAY(ary)->ptr[i]);
       }
       return result;
   }
}
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.