File: C:/Ruby27-x64/share/doc/ruby/html/SortedSet.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class SortedSet - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="Set.html">Set</a>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-SortedSet">
<h1 id="class-SortedSet" class="class">
class SortedSet
</h1>
<section class="description">
<p><a href="SortedSet.html"><code>SortedSet</code></a> implements a <a href="Set.html"><code>Set</code></a> that guarantees that its elements are yielded in sorted order (according to the return values of their #<=> methods) when iterating over them.</p>
<p>All elements that are added to a <a href="SortedSet.html"><code>SortedSet</code></a> must respond to the <=> method for comparison.</p>
<p>Also, all elements must be <em>mutually comparable</em>: <code>el1 <=> el2</code> must not return <code>nil</code> for any elements <code>el1</code> and <code>el2</code>, else an <a href="ArgumentError.html"><code>ArgumentError</code></a> will be raised when iterating over the <a href="SortedSet.html"><code>SortedSet</code></a>.</p>
<h2 id="class-SortedSet-label-Example">Example<span><a href="#class-SortedSet-label-Example">¶</a> <a href="#top">↑</a></span></h2>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">"set"</span>
<span class="ruby-identifier">set</span> = <span class="ruby-constant">SortedSet</span>.<span class="ruby-identifier">new</span>([<span class="ruby-value">2</span>, <span class="ruby-value">1</span>, <span class="ruby-value">5</span>, <span class="ruby-value">6</span>, <span class="ruby-value">4</span>, <span class="ruby-value">5</span>, <span class="ruby-value">3</span>, <span class="ruby-value">3</span>, <span class="ruby-value">3</span>])
<span class="ruby-identifier">ary</span> = []
<span class="ruby-identifier">set</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">ary</span> <span class="ruby-operator"><<</span> <span class="ruby-identifier">obj</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">ary</span> <span class="ruby-comment"># => [1, 2, 3, 4, 5, 6]</span>
<span class="ruby-identifier">set2</span> = <span class="ruby-constant">SortedSet</span>.<span class="ruby-identifier">new</span>([<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-string">"3"</span>])
<span class="ruby-identifier">set2</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">obj</span><span class="ruby-operator">|</span> } <span class="ruby-comment"># => raises ArgumentError: comparison of Fixnum with String failed</span>
</pre>
</section>
<section id="5Buntitled-5D" class="documentation-section">
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>