public Method

Array.unshift(...)

array.unshift(obj, ...)   array

Prepends objects to the front of array. other elements up one.

a = [ "b", "c", "d" ]
a.unshift("a")   #=> ["a", "b", "c", "d"]
a.unshift(1, 2)  #=> [ 1, 2, "a", "b", "c", "d"]

Source Code

/*
*  call-seq:
*     array.unshift(obj, ...)  -> array
*  
*  Prepends objects to the front of <i>array</i>.
*  other elements up one.
*     
*     a = [ "b", "c", "d" ]
*     a.unshift("a")   #=> ["a", "b", "c", "d"]
*     a.unshift(1, 2)  #=> [ 1, 2, "a", "b", "c", "d"]
*/

static VALUE
rb_ary_unshift_m(argc, argv, ary)
   int argc;
   VALUE *argv;
   VALUE ary;
{
   long len = RARRAY(ary)->len;

   if (argc == 0) return ary;

   /* make rooms by setting the last item */
   rb_ary_store(ary, len + argc - 1, Qnil);

   /* sliding items */
   MEMMOVE(RARRAY(ary)->ptr + argc, RARRAY(ary)->ptr, VALUE, len);
   MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);

   return ary;
}
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.