Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit fb13525

Browse files
committed
README
1 parent d26c6d6 commit fb13525

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
# Set
22

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.
43

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.
623

724
## Installation
825

@@ -22,7 +39,25 @@ Or install it yourself as:
2239

2340
## Usage
2441

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+
```
2661

2762
## Development
2863

@@ -32,5 +67,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
3267

3368
## Contributing
3469

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.
3671

0 commit comments

Comments
 (0)