Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Set#<=>
(untyped other) -> 0?
self <=> object → -1, 0, 1, or nil
set.c
View on GitHub ↗
static VALUE
set_i_compare(VALUE set, VALUE other)
{
if (rb_obj_is_kind_of(other, rb_cSet)) {
size_t set_size = RSET_SIZE(set);
size_t other_size = RSET_SIZE(other);
if (set_size < other_size) {
if (set_le(set, other) == Qtrue) {
return INT2NUM(-1);
}
}
else if (set_size > other_size) {
if (set_le(other, set) == Qtrue) {
return INT2NUM(1);
}
}
else if (set_le(set, other) == Qtrue) {
return INT2NUM(0);
}
}
return Qnil;
}
Compares self and object.
If object is another set, returns:
-
-1, ifselfis a proper subset ofobject. -
0, ifselfandobjecthave the same elements. -
1, ifselfis a proper superset ofobject. -
nil, if none of the above; that is, ifselfandobjecteach have one or more elements not included in the other.
Examples:
Example
set = Set[0, 1, 2] set <=> Set[3, 2, 1, 0] # => -1 set <=> Set[2, 1, 0] # => 0 set <=> Set[1, 0] # => 1 set <=> Set[1, 0, 3] # => nil
Returns nil if object is not a set:
Example
set <=> [2, 1, 0] # => nil # Array, not Set.
Related: see Methods for Comparing.