range_splitter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +42 -0
  2. data/lib/range_splitter.rb +21 -0
  3. metadata +80 -0
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ ## Range Splitter
2
+
3
+ Splits a range into multiple ranges.
4
+
5
+ ## Usage
6
+
7
+ Splits are made as evenly as possible. The larger ranges are placed at the beginning of the array.
8
+
9
+ ```ruby
10
+ (1..10).split
11
+ #=> [1..5, 6..10]
12
+
13
+ (1..9).split
14
+ #=> [1..5, 6..9]
15
+
16
+ (1..10).split(3)
17
+ #=> [1..4, 5..7, 8..10]
18
+
19
+ (1..9).split(3)
20
+ #=> [1..3, 4..6, 7..9]
21
+
22
+ (-5..5).split(5)
23
+ #=> [-5..-3, -2..-1, 0..1, 2..3, 4..5]
24
+
25
+ (1..3).split(100)
26
+ #=> [1..1, 2..2, 3..3]
27
+
28
+ (1..3).split(1)
29
+ #=> [1..3]
30
+
31
+ (1..3).split(0)
32
+ # ArgumentError: Cannot split 1..3 into 0 ranges.
33
+
34
+ (1..3).split(-3)
35
+ # ArgumentError: Cannot split 1..3 into -3 ranges.
36
+ ```
37
+
38
+ ## Contribution
39
+
40
+ Feel free to contribute. No commit is too small.
41
+
42
+ You should follow me: [@cpatuzzo](https://twitter.com/cpatuzzo)
@@ -0,0 +1,21 @@
1
+ class Range
2
+ def split(into = 2)
3
+ if into <= 0
4
+ err = "Cannot split #{self} into #{into} ranges."
5
+ raise ArgumentError.new(err)
6
+ elsif into == 1
7
+ [self]
8
+ else
9
+ partition = min + (count.to_f / into).ceil - 1
10
+
11
+ if max == partition
12
+ [self]
13
+ else
14
+ head = min..partition
15
+ tail = ((partition + 1)..max).split(into - 1)
16
+
17
+ [head] + tail
18
+ end
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: range_splitter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Christopher Patuzzo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-27 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Splits a range into multiple ranges.
35
+ email: chris@patuzzo.co.uk
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - README.md
44
+ - lib/range_splitter.rb
45
+ homepage: https://github.com/cpatuzzo/range_splitter
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.24
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Range Splitter
78
+ test_files: []
79
+
80
+ has_rdoc: