public Method

Array.&(p1)

array & other_array

Set Intersection—Returns a new array containing elements common to the two arrays, with no duplicates.

[ 1, 1, 3, 5 ] & [ 1, 2, 3 ]   #=> [ 1, 3 ]

Source Code

/* 
*  call-seq:
*     array & other_array
*
*  Set Intersection---Returns a new array
*  containing elements common to the two arrays, with no duplicates.
*
*     [ 1, 1, 3, 5 ] & [ 1, 2, 3 ]   #=> [ 1, 3 ]
*/


static VALUE
rb_ary_and(ary1, ary2)
   VALUE ary1, ary2;
{
   VALUE hash, ary3, v, vv;
   long i;

   ary2 = to_ary(ary2);
   ary3 = rb_ary_new2(RARRAY(ary1)->len < RARRAY(ary2)->len ?
           RARRAY(ary1)->len : RARRAY(ary2)->len);
   hash = ary_make_hash(ary2, 0);

   for (i=0; i<RARRAY(ary1)->len; i++) {
       v = vv = rb_ary_elt(ary1, i);
       if (st_delete(RHASH(hash)->tbl, (st_data_t*)&vv, 0)) {
           rb_ary_push(ary3, v);
       }
   }

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