Skip to content

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

Compares self and object.

If object is another set, returns:

  • -1, if self is a proper subset of object.

  • 0, if self and object have the same elements.

  • 1, if self is a proper superset of object.

  • nil, if none of the above; that is, if self and object each 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.

See also #<= #== #<< #=== also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0