Method not available on this version
This method is only available on newer versions.
The first available version (v2_6_3 ) is shown here.
difference (*args)
public
Array Difference
Returns a new array that is a copy of
the receiver, removing any items that also appear in any of the arrays
given as arguments. The order is preserved from the original array.
It compares elements using their #hash and
#eql? methods for efficiency.
[ 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 ]. difference ([ 1 , 2 , 4 ])
[ 1 , ' c ', :s , ' yep ' ]. difference ([ 1 ], [ ' a ', ' c ' ])
If you need set-like behavior, see the library class Set .
See also Array#-.
Show source static VALUE
rb_ary_difference_multi(int argc, VALUE *argv, VALUE ary)
{
VALUE ary_diff;
long i, length;
volatile VALUE t0;
bool *is_hash = ALLOCV_N(bool, t0, argc);
ary_diff = rb_ary_new();
length = RARRAY_LEN(ary);
for (i = 0; i < argc; i++) {
argv[i] = to_ary(argv[i]);
is_hash[i] = (length > SMALL_ARRAY_LEN && RARRAY_LEN(argv[i]) > SMALL_ARRAY_LEN);
if (is_hash[i]) argv[i] = ary_make_hash(argv[i]);
}
for (i = 0; i < RARRAY_LEN(ary); i++) {
int j;
VALUE elt = rb_ary_elt(ary, i);
for (j = 0; j < argc; j++){
if (is_hash[j]) {
if (rb_hash_stlike_lookup(argv[j], RARRAY_AREF(ary, i), NULL))
break;
}
else {
if (rb_ary_includes_by_eql(argv[j], elt)) break;
}
}
if (j == argc) rb_ary_push(ary_diff, elt);
}
ALLOCV_END(t0);
return ary_diff;
}