Class: SortedSet (Ruby 2.3.7)
Description: Class : SortedSet - Ruby 2.3.7

- Home
- Core 2.3.7
- Std-lib 2.3.7
- Downloads

Support for the Ruby 2.3 series has ended. See here for reference.

### Home Classes Methods

### In Files

- set.rb

### Parent

Set

### Class/Module Index

Quicksearch

- Enumerable
- Set
- SortedSet

No matching classes.

# SortedSet

SortedSet implements a Set that guarantees that its elements are yielded in sorted order (according to the return values of their #<=> methods) when iterating over them.

All elements that are added to a SortedSet must respond to the <=> method for comparison.

Also, all elements must be mutually comparable: `el1 <=> el2` must not return `nil` for any elements `el1` and `el2`, else an ArgumentError will be raised when iterating over the SortedSet.

## Example¶ ↑

    require "set"

    set = SortedSet.new([2, 1, 5, 6, 4, 5, 3, 3, 3])
    ary = []

    set.each do |obj|
      ary << obj
    end

    p ary # => [1, 2, 3, 4, 5, 6]

    set2 = SortedSet.new([1, 2, "3"])
    set2.each { |obj| } # => raises ArgumentError: comparison of Fixnum with String failed