Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If all you need the hash for is membership, consider using a <a href="http://ruby-doc.org/stdlib/libdoc/set/rdoc/Set.html" rel="noreferrer"><code>Set</code></a>:</p> <blockquote> <h1>Set</h1> <p><a href="http://ruby-doc.org/stdlib/libdoc/set/rdoc/Set.html" rel="noreferrer">Set</a> implements a collection of unordered values with no duplicates. This is a hybrid of Array's intuitive inter-operation facilities and Hash's fast lookup.</p> <p><a href="http://ruby-doc.org/stdlib/libdoc/set/rdoc/Set.html" rel="noreferrer">Set</a> is easy to use with <a href="http://ruby-doc.org/core/Enumerable.html" rel="noreferrer">Enumerable</a> objects (implementing <code>each</code>). Most of the initializer methods and binary operators accept generic <a href="http://ruby-doc.org/core/Enumerable.html" rel="noreferrer">Enumerable</a> objects besides sets and arrays. An <a href="http://ruby-doc.org/core/Enumerable.html" rel="noreferrer">Enumerable</a> object can be converted to <a href="http://ruby-doc.org/stdlib/libdoc/set/rdoc/Set.html" rel="noreferrer">Set</a> using the <a href="http://ruby-doc.org/stdlib-2.2.1/libdoc/set/rdoc/Enumerable.html#method-i-to_set" rel="noreferrer"><code>to_set</code></a> method.</p> <p><a href="http://ruby-doc.org/stdlib/libdoc/set/rdoc/Set.html" rel="noreferrer">Set</a> uses Hash as storage, so you must note the following points:</p> <ul> <li>Equality of elements is determined according to <code>Object#eql?</code> and <code>Object#hash</code>.</li> <li>Set assumes that the identity of each element does not change while it is stored. Modifying an element of a set will render the set to an unreliable state.</li> <li>When a string is to be stored, a frozen copy of the string is stored instead unless the original string is already frozen.</li> </ul> <h2>Comparison</h2> <p>The comparison operators <code>&lt;</code>, <code>&gt;</code>, <code>&lt;=</code> and <code>&gt;=</code> are implemented as shorthand for the {proper_,}{subset?,superset?} methods. However, the <code>&lt;=&gt;</code> operator is intentionally left out because not every pair of sets is comparable. ({x,y} vs. {x,z} for example)</p> <h2>Example</h2> <pre><code>require 'set' s1 = Set.new [1, 2] # -&gt; #&lt;Set: {1, 2}&gt; s2 = [1, 2].to_set # -&gt; #&lt;Set: {1, 2}&gt; s1 == s2 # -&gt; true s1.add("foo") # -&gt; #&lt;Set: {1, 2, "foo"}&gt; s1.merge([2, 6]) # -&gt; #&lt;Set: {1, 2, "foo", 6}&gt; s1.subset? s2 # -&gt; false s2.subset? s1 # -&gt; true </code></pre> <p>[...]</p> <h2>Public Class Methods</h2> <h3>new(enum = nil)</h3> <p>Creates a new set containing the elements of the given enumerable object.</p> <p>If a block is given, the elements of enum are preprocessed by the given block.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload