You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 15, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+39-4Lines changed: 39 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,25 @@
1
1
# Set
2
2
3
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/set`. To experiment with that code, run `bin/console` for an interactive prompt.
4
3
5
-
TODO: Delete this and the text above, and describe your gem
4
+
Set implements a collection of unordered values with no duplicates.
5
+
This is a hybrid of Array's intuitive inter-operation facilities and
6
+
Hash's fast lookup.
7
+
8
+
Set is easy to use with Enumerable objects (implementing +each+).
9
+
Most of the initializer methods and binary operators accept generic
10
+
Enumerable objects besides sets and arrays. An Enumerable object
11
+
can be converted to Set using the +to_set+ method.
12
+
13
+
Set uses Hash as storage, so you must note the following points:
14
+
15
+
* Equality of elements is determined according to Object#eql? and
16
+
Object#hash. Use Set#compare_by_identity to make a set compare
17
+
its elements by their identity.
18
+
* Set assumes that the identity of each element does not change
19
+
while it is stored. Modifying an element of a set will render the
20
+
set to an unreliable state.
21
+
* When a string is to be stored, a frozen copy of the string is
22
+
stored instead unless the original string is already frozen.
6
23
7
24
## Installation
8
25
@@ -22,7 +39,25 @@ Or install it yourself as:
22
39
23
40
## Usage
24
41
25
-
TODO: Write usage instructions here
42
+
### Comparison
43
+
44
+
The comparison operators <, >, <=, and >= are implemented as
45
+
shorthand for the {proper_,}{subset?,superset?} methods. However,
46
+
the <=> operator is intentionally left out because not every pair of
47
+
sets is comparable ({x, y} vs. {x, z} for example).
48
+
49
+
### Example
50
+
51
+
```ruby
52
+
require'set'
53
+
s1 =Set[1, 2] #=> #<Set: {1, 2}>
54
+
s2 = [1, 2].to_set #=> #<Set: {1, 2}>
55
+
s1 == s2 #=> true
56
+
s1.add("foo") #=> #<Set: {1, 2, "foo"}>
57
+
s1.merge([2, 6]) #=> #<Set: {1, 2, "foo", 6}>
58
+
s1.subset?(s2) #=> false
59
+
s2.subset?(s1) #=> true
60
+
```
26
61
27
62
## Development
28
63
@@ -32,5 +67,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
67
33
68
## Contributing
34
69
35
-
Bug reports and pull requests are welcome on GitHub at https://github.com/hsbset.
70
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/set.
0 commit comments