range_splitter 1.0.0 → 2.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 +19 -8
  2. data/lib/range_splitter.rb +16 -3
  3. metadata +4 -4
data/README.md CHANGED
@@ -4,34 +4,45 @@ Splits a range into multiple ranges.
4
4
 
5
5
  ## Usage
6
6
 
7
- Splits are made as evenly as possible. The larger ranges are placed at the beginning of the array.
7
+ Splits are made as evenly as possible. The larger ranges are placed at the beginning of the array unless the endianness is changed.
8
8
 
9
9
  ```ruby
10
+ require 'range_splitter'
11
+
10
12
  (1..10).split
11
13
  #=> [1..5, 6..10]
12
14
 
13
15
  (1..9).split
14
16
  #=> [1..5, 6..9]
15
17
 
16
- (1..10).split(3)
18
+ (1..9).split(:endianness => :little)
19
+ #=> [1..4, 5..9]
20
+
21
+ (1..10).split(:into => 3)
17
22
  #=> [1..4, 5..7, 8..10]
18
23
 
19
- (1..9).split(3)
24
+ (1..9).split(:into => 3)
20
25
  #=> [1..3, 4..6, 7..9]
21
26
 
22
- (-5..5).split(5)
27
+ (-5..5).split(:into => 5)
23
28
  #=> [-5..-3, -2..-1, 0..1, 2..3, 4..5]
24
29
 
25
- (1..3).split(100)
30
+ (5..8).split(:into => 3, :endianness => :little)
31
+ #=> [5..5, 6..6, 7..8]
32
+
33
+ (1..3).split(:into => 100)
26
34
  #=> [1..1, 2..2, 3..3]
27
35
 
28
- (1..3).split(1)
36
+ (1..1).split
37
+ #=> [1..1]
38
+
39
+ (1..3).split(:into => 1)
29
40
  #=> [1..3]
30
41
 
31
- (1..3).split(0)
42
+ (1..3).split(:into => 0)
32
43
  # ArgumentError: Cannot split 1..3 into 0 ranges.
33
44
 
34
- (1..3).split(-3)
45
+ (1..3).split(:into => -3)
35
46
  # ArgumentError: Cannot split 1..3 into -3 ranges.
36
47
  ```
37
48
 
@@ -1,9 +1,19 @@
1
1
  class Range
2
- def split(into = 2)
2
+ def split(params = {})
3
+ into = params[:into] || 2
4
+ endianness = params[:endianness] || :big
5
+
6
+ unless [:big, :little].include?(endianness)
7
+ err = 'The endianness parameter must be either :big or :little'
8
+ raise ArgumentError.new(err)
9
+ end
10
+
3
11
  if into <= 0
4
12
  err = "Cannot split #{self} into #{into} ranges."
5
13
  raise ArgumentError.new(err)
6
- elsif into == 1
14
+ end
15
+
16
+ if into == 1
7
17
  [self]
8
18
  else
9
19
  partition = min + (count.to_f / into).ceil - 1
@@ -11,8 +21,11 @@ class Range
11
21
  if max == partition
12
22
  [self]
13
23
  else
24
+ args = params.merge(:into => into - 1)
25
+ partition -= 1 if endianness == :little
26
+
14
27
  head = min..partition
15
- tail = ((partition + 1)..max).split(into - 1)
28
+ tail = ((partition + 1)..max).split(args)
16
29
 
17
30
  [head] + tail
18
31
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: range_splitter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
- - 1
7
+ - 2
8
8
  - 0
9
9
  - 0
10
- version: 1.0.0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Christopher Patuzzo
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-27 00:00:00 Z
18
+ date: 2012-11-01 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec