public Method

Array.|(p1)

array | other_array       an_array

Set Union—Returns a new array by joining this array with

other_array, removing duplicates.

   [ "a", "b", "c" ] | [ "c", "d", "a" ]

> [ "a", "b", "c", "d" ]

Source Code

/* 
*  call-seq:
*     array | other_array     ->  an_array
*
*  Set Union---Returns a new array by joining this array with
*  other_array, removing duplicates.
*
*     [ "a", "b", "c" ] | [ "c", "d", "a" ]
*            #=> [ "a", "b", "c", "d" ]
*/

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

   ary2 = to_ary(ary2);
   ary3 = rb_ary_new2(RARRAY(ary1)->len+RARRAY(ary2)->len);
   hash = ary_make_hash(ary1, ary2);

   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);
       }
   }
   for (i=0; i<RARRAY(ary2)->len; i++) {
       v = vv = rb_ary_elt(ary2, 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.