multi_range 1.3.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,219 +1,238 @@
1
- # MultiRange
2
-
3
- [![Gem Version](https://img.shields.io/gem/v/multi_range.svg?style=flat)](http://rubygems.org/gems/multi_range)
4
- [![Build Status](https://travis-ci.com/khiav223577/multi_range.svg?branch=master)](https://travis-ci.org/khiav223577/multi_range)
5
- [![RubyGems](http://img.shields.io/gem/dt/multi_range.svg?style=flat)](http://rubygems.org/gems/multi_range)
6
- [![Code Climate](https://codeclimate.com/github/khiav223577/multi_range/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/multi_range)
7
- [![Test Coverage](https://codeclimate.com/github/khiav223577/multi_range/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/multi_range/coverage)
8
-
9
- ## Supports
10
- - Ruby 1.8 ~ 2.7
11
-
12
- ## Installation
13
-
14
- ```ruby
15
- gem 'multi_range'
16
- ```
17
-
18
- And then execute:
19
-
20
- $ bundle
21
-
22
- Or install it yourself as:
23
-
24
- $ gem install multi_range
25
-
26
- ## Usage
27
-
28
- Allow you to manipulate a group of ranges. Such as merging overlapping ranges, doing ranges union and difference.
29
-
30
- ### Sample a number
31
- ```rb
32
- multi_range = MultiRange.new([1..5, 10..12])
33
- multi_range.sample
34
- # => equals to [1, 2, 3, 4, 5, 10, 11, 12].sample
35
- ```
36
-
37
- ### Difference of ranges
38
- ```rb
39
- multi_range = MultiRange.new([1..10])
40
- multi_range -= 5..7
41
- multi_range.ranges
42
- # => [1...5, 8..10]
43
- ```
44
-
45
- ```rb
46
- multi_range = MultiRange.new([1..10, 50..70, 80..100])
47
- multi_range -= 5..85
48
- multi_range.ranges
49
- # => [1...5, 86..100]
50
- ```
51
-
52
- ```rb
53
- multi_range = MultiRange.new([1..10, 50..70, 80..100])
54
- multi_range -= MultiRange.new([5..60, 75..85])
55
- multi_range.ranges
56
- # => [1...5, 61..70, 86..100]
57
- ```
58
-
59
- ### Union ranges
60
-
61
- ```rb
62
- multi_range = MultiRange.new([1..5])
63
- multi_range |= 3..8
64
- multi_range.ranges
65
- # => [1..8]
66
- ```
67
-
68
- ```rb
69
- multi_range = MultiRange.new([1..5, 10..15, 20..25])
70
- multi_range |= MultiRange.new([3..6, 14..22, 30])
71
- multi_range.ranges
72
- # => [1..6, 10..25, 30..30]
73
- ```
74
-
75
- ### Merge overlaps
76
- ```rb
77
- multi_range = MultiRange.new([1, 2, 4..6, 7, 8..12])
78
- multi_range.merge_overlaps.ranges
79
- # => [1..2, 4..12]
80
-
81
- multi_range = MultiRange.new([1.2..1.5, 1.7..1.9, 1.8..2.2])
82
- multi_range.merge_overlaps.ranges
83
- # => [1.2..1.5, 1.7..2.2]
84
- ```
85
-
86
- ### Check if it overlaps with the other
87
-
88
- ```rb
89
- multi_range = MultiRange.new([1..5, 10..15, 20..25])
90
- multi_range.overlaps?(7..8)
91
- # => false
92
-
93
- multi_range.overlaps?(3..8)
94
- # => true
95
-
96
- multi_range.overlaps?(7..12)
97
- # => true
98
- ```
99
-
100
- ```rb
101
- multi_range = MultiRange.new([1..5, 10..15, 20..25])
102
- multi_range.overlaps?(MultiRange.new([6..8, 18..22]))
103
- # => true
104
- ```
105
-
106
-
107
- ### Check if it contains overlaps
108
-
109
- ```rb
110
- MultiRange.new([0..3, 5..10, 20..50]).contain_overlaps?
111
- # => false
112
-
113
- MultiRange.new([0...5, 5..10, 20..50]).contain_overlaps?
114
- # => false
115
-
116
- MultiRange.new([0..5, 5..10, 20..50]).contain_overlaps?
117
- # => true
118
-
119
- MultiRange.new([0...7, 5..10, 20..50]).contain_overlaps?
120
- # => true
121
- ```
122
-
123
- ### Range-like interface
124
-
125
- #### each
126
- ```rb
127
- MultiRange.new([1..3, 6, 8..9]).each{|s| print s }
128
- # => 123689
129
- ```
130
-
131
- #### map
132
- ```rb
133
- MultiRange.new([1..3, 6, 8..9]).map{|s| s * 2 }
134
- # => [2, 4, 6, 12, 16, 18]
135
- ```
136
-
137
- #### index_with
138
- ```rb
139
- MultiRange.new([1..3, 6, 8..9]).index_with(true)
140
- # => { 1 => true, 2 => true, 3 => true, 6 => true, 8 => true, 9 => true }
141
- ```
142
-
143
- #### min
144
- ```rb
145
- MultiRange.new([1..3, 6, 8..9]).min
146
- # => 1
147
- ```
148
-
149
- #### max
150
- ```rb
151
- MultiRange.new([1..3, 6, 8..9]).max
152
- # => 9
153
- ```
154
-
155
- #### to_a
156
- ```rb
157
- MultiRange.new([1..3, 6, 8..9]).to_a
158
- # => [1, 2, 3, 6, 8, 9]
159
- ```
160
-
161
-
162
- #### size
163
- ```rb
164
- MultiRange.new([1..3, 6, 8..9]).size
165
- # => 6
166
- ```
167
-
168
- ### Warning
169
-
170
- The return value may be different when there are some overlapped ranges.
171
- Call `merge_overlaps` if you want to merge overlapped ranges.
172
-
173
- ```rb
174
- MultiRange.new([1..5, 3..6]).to_a
175
- # => [1, 2, 3, 4, 5, 3, 4, 5, 6]
176
-
177
- MultiRange.new([1..5, 3..6]).merge_overlaps.to_a
178
- # => [1, 2, 3, 4, 5, 6]
179
- ```
180
-
181
- ```rb
182
- MultiRange.new([1..5, 3..6]).each{|s| print s }
183
- # => 123453456
184
-
185
- MultiRange.new([1..5, 3..6]).merge_overlaps.each{|s| print s }
186
- # => 123456
187
- ```
188
-
189
- ```rb
190
- MultiRange.new([1..5, 3..6]).map{|s| s * 2 }
191
- # => [2, 4, 6, 8, 10, 6, 8, 10, 12]
192
-
193
- MultiRange.new([1..5, 3..6]).merge_overlaps.map{|s| s * 2 }
194
- # => [2, 4, 6, 8, 10, 12]
195
- ```
196
-
197
- ```rb
198
- MultiRange.new([1..5, 3..6]).size
199
- # => 9
200
-
201
- MultiRange.new([1..5, 3..6]).merge_overlaps.size
202
- # => 6
203
- ```
204
-
205
- ## Development
206
-
207
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
208
-
209
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
210
-
211
- ## Contributing
212
-
213
- Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/multi_range. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
214
-
215
-
216
- ## License
217
-
218
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
219
-
1
+ # MultiRange
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/multi_range.svg?style=flat)](http://rubygems.org/gems/multi_range)
4
+ [![Build Status](https://travis-ci.com/khiav223577/multi_range.svg?branch=master)](https://travis-ci.org/khiav223577/multi_range)
5
+ [![RubyGems](http://img.shields.io/gem/dt/multi_range.svg?style=flat)](http://rubygems.org/gems/multi_range)
6
+ [![Code Climate](https://codeclimate.com/github/khiav223577/multi_range/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/multi_range)
7
+ [![Test Coverage](https://codeclimate.com/github/khiav223577/multi_range/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/multi_range/coverage)
8
+
9
+ ## Supports
10
+
11
+ - Ruby 2.0 ~ 2.7
12
+
13
+ For Ruby 1.8.x and 1.9.x, please use multi_range < 2.
14
+
15
+ ## Installation
16
+
17
+ ```ruby
18
+ gem 'multi_range'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install multi_range
28
+
29
+ ## Usage
30
+
31
+ Allow you to manipulate a group of ranges. Such as merging overlapping ranges, doing ranges union, intersection, and difference.
32
+
33
+ ### Sample a number
34
+ ```rb
35
+ multi_range = MultiRange.new([1..5, 10..12])
36
+ multi_range.sample
37
+ # => equals to [1, 2, 3, 4, 5, 10, 11, 12].sample
38
+ ```
39
+
40
+ ### Difference of ranges
41
+ ```rb
42
+ multi_range = MultiRange.new([1..10])
43
+ multi_range -= 5..7
44
+ multi_range.ranges
45
+ # => [1...5, 8..10]
46
+ ```
47
+
48
+ ```rb
49
+ multi_range = MultiRange.new([1..10, 50..70, 80..100])
50
+ multi_range -= 5..85
51
+ multi_range.ranges
52
+ # => [1...5, 86..100]
53
+ ```
54
+
55
+ ```rb
56
+ multi_range = MultiRange.new([1..10, 50..70, 80..100])
57
+ multi_range -= MultiRange.new([5..60, 75..85])
58
+ multi_range.ranges
59
+ # => [1...5, 61..70, 86..100]
60
+ ```
61
+
62
+ ### Union ranges
63
+
64
+ ```rb
65
+ multi_range = MultiRange.new([1..5])
66
+ multi_range |= 3..8
67
+ multi_range.ranges
68
+ # => [1..8]
69
+ ```
70
+
71
+ ```rb
72
+ multi_range = MultiRange.new([1..5, 10..15, 20..25])
73
+ multi_range |= MultiRange.new([3..6, 14..22, 30])
74
+ multi_range.ranges
75
+ # => [1..6, 10..25, 30..30]
76
+ ```
77
+
78
+ ### Intersection ranges
79
+
80
+ ```rb
81
+ multi_range = MultiRange.new([1..5])
82
+ multi_range &= 3..8
83
+ multi_range.ranges
84
+ # => [3..5]
85
+ ```
86
+
87
+ ```rb
88
+ multi_range = MultiRange.new([1..3, 5..10])
89
+ multi_range &= MultiRange.new([2..6, 8..9])
90
+ multi_range.ranges
91
+ # => [2..3, 5..6, 8..9]
92
+ ```
93
+
94
+ ### Merge overlaps
95
+ ```rb
96
+ multi_range = MultiRange.new([1, 2, 4..6, 7, 8..12])
97
+ multi_range.merge_overlaps.ranges
98
+ # => [1..2, 4..12]
99
+
100
+ multi_range = MultiRange.new([1.2..1.5, 1.7..1.9, 1.8..2.2])
101
+ multi_range.merge_overlaps.ranges
102
+ # => [1.2..1.5, 1.7..2.2]
103
+ ```
104
+
105
+ ### Check if it overlaps with the other
106
+
107
+ ```rb
108
+ multi_range = MultiRange.new([1..5, 10..15, 20..25])
109
+ multi_range.overlaps?(7..8)
110
+ # => false
111
+
112
+ multi_range.overlaps?(3..8)
113
+ # => true
114
+
115
+ multi_range.overlaps?(7..12)
116
+ # => true
117
+ ```
118
+
119
+ ```rb
120
+ multi_range = MultiRange.new([1..5, 10..15, 20..25])
121
+ multi_range.overlaps?(MultiRange.new([6..8, 18..22]))
122
+ # => true
123
+ ```
124
+
125
+
126
+ ### Check if it contains overlaps
127
+
128
+ ```rb
129
+ MultiRange.new([0..3, 5..10, 20..50]).contain_overlaps?
130
+ # => false
131
+
132
+ MultiRange.new([0...5, 5..10, 20..50]).contain_overlaps?
133
+ # => false
134
+
135
+ MultiRange.new([0..5, 5..10, 20..50]).contain_overlaps?
136
+ # => true
137
+
138
+ MultiRange.new([0...7, 5..10, 20..50]).contain_overlaps?
139
+ # => true
140
+ ```
141
+
142
+ ### Range-like interface
143
+
144
+ #### each
145
+ ```rb
146
+ MultiRange.new([1..3, 6, 8..9]).each{|s| print s }
147
+ # => 123689
148
+ ```
149
+
150
+ #### map
151
+ ```rb
152
+ MultiRange.new([1..3, 6, 8..9]).map{|s| s * 2 }
153
+ # => [2, 4, 6, 12, 16, 18]
154
+ ```
155
+
156
+ #### index_with
157
+ ```rb
158
+ MultiRange.new([1..3, 6, 8..9]).index_with(true)
159
+ # => { 1 => true, 2 => true, 3 => true, 6 => true, 8 => true, 9 => true }
160
+ ```
161
+
162
+ #### min
163
+ ```rb
164
+ MultiRange.new([1..3, 6, 8..9]).min
165
+ # => 1
166
+ ```
167
+
168
+ #### max
169
+ ```rb
170
+ MultiRange.new([1..3, 6, 8..9]).max
171
+ # => 9
172
+ ```
173
+
174
+ #### to_a
175
+ ```rb
176
+ MultiRange.new([1..3, 6, 8..9]).to_a
177
+ # => [1, 2, 3, 6, 8, 9]
178
+ ```
179
+
180
+
181
+ #### size
182
+ ```rb
183
+ MultiRange.new([1..3, 6, 8..9]).size
184
+ # => 6
185
+ ```
186
+
187
+ ### Warning
188
+
189
+ The return value may be different when there are some overlapped ranges.
190
+ Call `merge_overlaps` if you want to merge overlapped ranges.
191
+
192
+ ```rb
193
+ MultiRange.new([1..5, 3..6]).to_a
194
+ # => [1, 2, 3, 4, 5, 3, 4, 5, 6]
195
+
196
+ MultiRange.new([1..5, 3..6]).merge_overlaps.to_a
197
+ # => [1, 2, 3, 4, 5, 6]
198
+ ```
199
+
200
+ ```rb
201
+ MultiRange.new([1..5, 3..6]).each{|s| print s }
202
+ # => 123453456
203
+
204
+ MultiRange.new([1..5, 3..6]).merge_overlaps.each{|s| print s }
205
+ # => 123456
206
+ ```
207
+
208
+ ```rb
209
+ MultiRange.new([1..5, 3..6]).map{|s| s * 2 }
210
+ # => [2, 4, 6, 8, 10, 6, 8, 10, 12]
211
+
212
+ MultiRange.new([1..5, 3..6]).merge_overlaps.map{|s| s * 2 }
213
+ # => [2, 4, 6, 8, 10, 12]
214
+ ```
215
+
216
+ ```rb
217
+ MultiRange.new([1..5, 3..6]).size
218
+ # => 9
219
+
220
+ MultiRange.new([1..5, 3..6]).merge_overlaps.size
221
+ # => 6
222
+ ```
223
+
224
+ ## Development
225
+
226
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
227
+
228
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
229
+
230
+ ## Contributing
231
+
232
+ Bug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/multi_range. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
233
+
234
+
235
+ ## License
236
+
237
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
238
+
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- require 'bundler/gem_tasks'
2
- require 'rake/testtask'
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << 'test'
6
- t.libs << 'lib'
7
- t.test_files = FileList['test/**/*_test.rb']
8
- end
9
-
10
- task :default => :test
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'multi_range'
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require 'irb'
14
- IRB.start
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'multi_range'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install --gemfile=gemfiles/4.2.gemfile
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install --gemfile=gemfiles/Gemfile
7
+
8
+ # Do any other automated setup that you need to do here
data/gemfiles/Gemfile CHANGED
@@ -1,9 +1,9 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in rails_or.gemspec
4
-
5
- group :test do
6
- gem 'simplecov'
7
- end
8
-
9
- gemspec :path => '../'
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_or.gemspec
4
+
5
+ group :test do
6
+ gem 'simplecov'
7
+ end
8
+
9
+ gemspec :path => '../'
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- class MultiRange
4
- VERSION = '1.3.2'
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ class MultiRange
4
+ VERSION = '2.0.0'
5
+ end