instance method
Set#intersection
(::_Each[A]) -> self
intersection(p1)
set.c
View on GitHub ↗
static VALUE
set_i_intersection(VALUE set, VALUE other)
{
VALUE new_set = set_s_alloc(rb_obj_class(set));
set_table *stable = RSET_TABLE(set);
set_table *ntable = RSET_TABLE(new_set);
if (rb_obj_is_kind_of(other, rb_cSet)) {
set_table *otable = RSET_TABLE(other);
if (set_table_size(stable) >= set_table_size(otable)) {
/* Swap so we iterate over the smaller set */
otable = stable;
set = other;
}
struct set_intersection_data data = {
.set = new_set,
.into = ntable,
.other = otable
};
set_iter(set, set_intersection_i, (st_data_t)&data);
}
else {
struct set_intersection_data data = {
.set = new_set,
.into = ntable,
.other = stable
};
rb_block_call(other, enum_method_id(other), 0, 0, set_intersection_block, (VALUE)&data);
}
return new_set;
}
Alias of #&
Returns a new set containing elements common to the set and the given enumerable object.
Example
Set[1, 3, 5] & Set[3, 2, 1] #=> Set[3, 1] Set['a', 'b', 'z'] & ['a', 'b', 'c'] #=> Set["a", "b"]