reduce_ranges 0.9.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 539f24c7e868cd7abf575714eb7fb11137623943
4
- data.tar.gz: ca87fef8d2ce13b1c22d02c471556c8f610344f4
3
+ metadata.gz: 4e5ed4f039e700600468cf12845b3b69aac354f2
4
+ data.tar.gz: e4bb98db6d0eae3a67edc78b3aa33f5e4b0708b7
5
5
  SHA512:
6
- metadata.gz: 92b5db2e60d597870a77f9b43278133d15b1b4bb0e91e07e8b0dda38d38c91286eaaa7c8422c05e79b4ae062538885df17bcdb92ecfc920bd387f4d4eda91280
7
- data.tar.gz: cfd6788a3e2e452ccff507d82a2cd72ffae12afdf7b92a1504c5aca70bc13aa9272197eeebb2d558bed6faea0ca8fe07a4c9249c9b79fcae395dcbfece327216
6
+ metadata.gz: da2a58c7d36e5201e34f4791b31cd4c91742e58c531c4863f054b8e03747c56a08a55c548aef5d2cd8a1a8ac788cef30f9df8c1786901a9937e5db3dab8065e6
7
+ data.tar.gz: 263ed93ac5922bd9058ed43ac13ce6e17ec72400df8465acfe09c9f1f6f94c50ea8e2b36326d711a2e69e445aaaf8a815ec0ef3426deaeda773fb505b43c4801
data/README.md CHANGED
@@ -30,6 +30,8 @@ Note that this only works on arrays of integers. If you call it on an array with
30
30
  # etc..
31
31
  ```
32
32
 
33
+ Shout outs to @Calyhre for the current implementation and @lkdjiin for his blog posts on the subject!
34
+
33
35
  ## Contributing
34
36
 
35
37
  1. Fork it ( https://github.com/jacobaweiss/reduce_ranges/fork )
data/lib/reduce_ranges.rb CHANGED
@@ -2,17 +2,15 @@ require 'reduce_ranges/version'
2
2
 
3
3
  class Array
4
4
  def reduce_ranges
5
- raise TypeError unless all? { |el| el.is_a?(Fixnum) }
5
+ raise TypeError unless all? { |el| el.is_a?(Fixnum) }
6
6
 
7
- arr = self.dup
8
- arr.each_with_index do |el, index|
9
- range_index = index + 1
10
- prev = el
11
- while arr[range_index] == prev + 1 do
12
- prev = arr[range_index]
13
- range_index += 1
14
- end
15
- arr[index..range_index-1] = (arr[index]..arr[range_index-1]) unless index == range_index - 1
7
+ results = []
8
+ temp = [self[0]]
9
+ self[1..-1].each do |e|
10
+ temp << e && next if temp.last == e - 1
11
+ results << (temp[1] ? (temp[0]..temp.last) : temp[0])
12
+ temp = [e]
16
13
  end
14
+ results << (temp[1] ? (temp[0]..temp.last) : temp[0])
17
15
  end
18
16
  end
@@ -1,3 +1,3 @@
1
1
  module ReduceRanges
2
- VERSION = '0.9.0'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reduce_ranges
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "@jacobaweiss"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-26 00:00:00.000000000 Z
11
+ date: 2014-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler