Method: Array#intersection

Defined in:
array.c

#intersection(other_ary1, other_ary2, ...) ⇒ Array

Set Intersection — Returns a new array containing unique elements common to self and other_arys. Order is preserved from the original array.

It compares elements using their #hash and #eql? methods for efficiency.

[ 1, 1, 3, 5 ].intersection([ 3, 2, 1 ])                    # => [ 1, 3 ]
[ "a", "b", "z" ].intersection([ "a", "b", "c" ], [ "b" ])  # => [ "b" ]
[ "a" ].intersection #=> [ "a" ]

See also Array#&.

Returns:



4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
# File 'array.c', line 4680

static VALUE
rb_ary_intersection_multi(int argc, VALUE *argv, VALUE ary)
{
    VALUE result = rb_ary_dup(ary);
    int i;

    for (i = 0; i < argc; i++) {
        result = rb_ary_and(result, argv[i]);
    }

    return result;
}