multi_range 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/README.md +44 -3
- data/lib/multi_range/version.rb +1 -1
- data/lib/multi_range.rb +26 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a51f5f8289e43754c02b2b3decfc55795c4f69ee43cf8acca25738167545c1e2
|
4
|
+
data.tar.gz: 11fec578c3d1f84d86085444eb876256dddbf352cd46958d21d1155f66b900a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 765a78073272b2b1fed76f2ddf0ba5ad968bba263aa1c06c36bd16412b87b3a5296cb5522b7b2226740ba45b7ac15028de802cc73c388f4ba6497a1f76219ad3
|
7
|
+
data.tar.gz: e3e7c19484f4b4532e39ece23d9ea1b7af6b8ea505ae5fd0a503177429370549fe3f531cf4e0575830d9de688fe9980872615bd5e71645b03e64689789abeceb
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -8,12 +8,9 @@
|
|
8
8
|
|
9
9
|
## Supports
|
10
10
|
- Ruby 1.8 ~ 2.7
|
11
|
-
- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0
|
12
11
|
|
13
12
|
## Installation
|
14
13
|
|
15
|
-
Provides cross-rails methods for you to upgrade rails, backport features, create easy-to-maintain gems, and so on.
|
16
|
-
|
17
14
|
```ruby
|
18
15
|
gem 'multi_range'
|
19
16
|
```
|
@@ -28,6 +25,50 @@ Or install it yourself as:
|
|
28
25
|
|
29
26
|
## Usage
|
30
27
|
|
28
|
+
Allow you to manipulate a group of ranges.
|
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
|
+
### Range difference
|
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
|
+
### Range-like interface
|
53
|
+
|
54
|
+
#### each
|
55
|
+
```rb
|
56
|
+
MultiRange([1..3, 5..6]).each{|s| print s }
|
57
|
+
# => 12356
|
58
|
+
```
|
59
|
+
|
60
|
+
#### map
|
61
|
+
```rb
|
62
|
+
MultiRange([1..3, 5..6]).map{|s| s * 2 }
|
63
|
+
# => [2, 4, 6, 10, 12]
|
64
|
+
```
|
65
|
+
|
66
|
+
#### index_with
|
67
|
+
```rb
|
68
|
+
MultiRange([1..3, 5..6]).index_with(true)
|
69
|
+
# => { 1 => true, 2 => true, 3 => true, 5 => true, 6 => true }
|
70
|
+
```
|
71
|
+
|
31
72
|
|
32
73
|
## Development
|
33
74
|
|
data/lib/multi_range/version.rb
CHANGED
data/lib/multi_range.rb
CHANGED
@@ -15,10 +15,12 @@ if not Enumerable.method_defined?(:to_h)
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class MultiRange
|
18
|
+
INDEX_WITH_DEFAULT = Object.new
|
19
|
+
|
18
20
|
attr_reader :ranges
|
19
21
|
|
20
22
|
def initialize(ranges) # range 要由小到大排序,且各 range 不能重疊
|
21
|
-
@ranges = ranges
|
23
|
+
@ranges = ranges.map{|s| s.is_a?(Integer) ? s..s : s }
|
22
24
|
end
|
23
25
|
|
24
26
|
def -(other)
|
@@ -61,4 +63,27 @@ class MultiRange
|
|
61
63
|
def any?
|
62
64
|
@ranges.any?
|
63
65
|
end
|
66
|
+
|
67
|
+
def index_with(default = INDEX_WITH_DEFAULT)
|
68
|
+
if block_given?
|
69
|
+
fail ArgumentError.new('wrong number of arguments (given 1, expected 0)') if default != INDEX_WITH_DEFAULT
|
70
|
+
return map{|s| [s, yield(s)] }.to_h
|
71
|
+
end
|
72
|
+
|
73
|
+
return to_enum(:index_with){ size } if default == INDEX_WITH_DEFAULT
|
74
|
+
return map{|s| [s, default] }.to_h
|
75
|
+
end
|
76
|
+
|
77
|
+
def each
|
78
|
+
return to_enum(:each){ size } if !block_given?
|
79
|
+
|
80
|
+
ranges.each do |range|
|
81
|
+
range.each{|s| yield(s) }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def map
|
86
|
+
return to_enum(:map){ size } if !block_given?
|
87
|
+
return each.map{|s| yield(s) }
|
88
|
+
end
|
64
89
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multi_range
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|