public Method

Array.pop

array.pop   obj or nil

Removes the last element from self and returns it, or nil if the array is empty.

a = [ "a", "m", "z" ]
a.pop   #=> "z"
a       #=> ["a", "m"]

Source Code

/*
*  call-seq:
*     array.pop  -> obj or nil
*  
*  Removes the last element from <i>self</i> and returns it, or
*  <code>nil</code> if the array is empty.
*     
*     a = [ "a", "m", "z" ]
*     a.pop   #=> "z"
*     a       #=> ["a", "m"]
*/

VALUE
rb_ary_pop(ary)
   VALUE ary;
{
   rb_ary_modify_check(ary);
   if (RARRAY(ary)->len == 0) return Qnil;
   if (!FL_TEST(ary, ELTS_SHARED) &&
           RARRAY(ary)->len * 2 < RARRAY(ary)->aux.capa &&
           RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) {
       RARRAY(ary)->aux.capa = RARRAY(ary)->len * 2;
       REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
   }
   return RARRAY(ary)->ptr[--RARRAY(ary)->len];
}
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.