range_slice 0.1.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.
Binary file
@@ -0,0 +1,4 @@
1
+ == 0.1.0 / 2008-04-16
2
+
3
+ * Released!
4
+
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/range_slice.rb
6
+ test/range_slice_spec.rb
@@ -0,0 +1,48 @@
1
+ RangeSlice
2
+ by Mike Judge
3
+ rubysideshow.com
4
+
5
+ == SYNOPSIS:
6
+
7
+ (1..1000).slice(300)
8
+ # => [1..300, 301..600, 601..900, 901..1000]
9
+
10
+ require 'active_support'
11
+ (7.days.ago..Time.now).slice(24.hours)
12
+ # => [thu apr 10 17:48:08 -0700 2008..fri apr 11 17:48:07 -0700 2008,
13
+ fri apr 11 17:48:08 -0700 2008..sat apr 12 17:48:07 -0700 2008,
14
+ sat apr 12 17:48:08 -0700 2008..sun apr 13 17:48:07 -0700 2008,
15
+ sun apr 13 17:48:08 -0700 2008..mon apr 14 17:48:07 -0700 2008,
16
+ mon apr 14 17:48:08 -0700 2008..tue apr 15 17:48:07 -0700 2008,
17
+ tue apr 15 17:48:08 -0700 2008..wed apr 16 17:48:07 -0700 2008,
18
+ wed apr 16 17:48:08 -0700 2008..thu apr 17 17:48:07 -0700 2008,
19
+ thu apr 17 17:48:08 -0700 2008..thu apr 17 17:48:08 -0700 2008]
20
+
21
+ == INSTALL:
22
+
23
+ * sudo gem install range_slice
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2008 Mike Judge
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/range_slice.rb'
6
+
7
+ Hoe.new('range_slice', RangeSlice::VERSION) do |p|
8
+ p.name = 'range_slice'
9
+ p.rubyforge_name = 'rubysideshow'
10
+ p.author = 'Mike Judge'
11
+ p.email = 'mikelovesrobots@gmail.com'
12
+ p.summary = 'Adds Range#slice'
13
+ p.description = p.paragraphs_of('README.txt', 1..5).join("\n\n")
14
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
15
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
16
+ end
17
+
18
+ # vim: syntax=Ruby
@@ -0,0 +1,48 @@
1
+ class RangeSlice
2
+ VERSION = '0.1.0'
3
+ end
4
+
5
+ class Range
6
+ # Slices the range into smaller ranges of a given increment.
7
+ #
8
+ # (1..1000).slice(300)
9
+ # # => [1..300, 301..600, 601..900, 901..1000]
10
+ #
11
+ # require 'active_support'
12
+ # (7.days.ago..Time.now).slice(24.hours)
13
+ # # => [Thu Apr 10 17:48:08 -0700 2008..Fri Apr 11 17:48:07 -0700 2008,
14
+ # Fri Apr 11 17:48:08 -0700 2008..Sat Apr 12 17:48:07 -0700 2008,
15
+ # Sat Apr 12 17:48:08 -0700 2008..Sun Apr 13 17:48:07 -0700 2008,
16
+ # Sun Apr 13 17:48:08 -0700 2008..Mon Apr 14 17:48:07 -0700 2008,
17
+ # Mon Apr 14 17:48:08 -0700 2008..Tue Apr 15 17:48:07 -0700 2008,
18
+ # Tue Apr 15 17:48:08 -0700 2008..Wed Apr 16 17:48:07 -0700 2008,
19
+ # Wed Apr 16 17:48:08 -0700 2008..Thu Apr 17 17:48:07 -0700 2008,
20
+ # Thu Apr 17 17:48:08 -0700 2008..Thu Apr 17 17:48:08 -0700 2008]
21
+ #
22
+ # Note: This method like most built-in range methods, doesn't work with
23
+ # descending ranges (e.g., (1000..1).include? 2 returns false)
24
+ #
25
+ def slice(increment)
26
+ start_values = [first]
27
+ while self.include?(next_value = start_values.last + increment)
28
+ start_values << next_value
29
+ end
30
+
31
+ end_values = []
32
+ start_values.each do |start_value|
33
+ end_value = start_value + increment - 1
34
+ unless self.include? end_value
35
+ end_value = self.last
36
+ end
37
+ end_values << end_value
38
+ end
39
+
40
+ result = []
41
+ start_values.each_with_index do |start_value, index|
42
+ end_value = end_values[index]
43
+ result << (start_value..end_value)
44
+ end
45
+
46
+ result
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'range_slice')
3
+
4
+ def slice_test(range, size, results)
5
+ describe "when sliced into chunks of #{size}" do
6
+ before(:all) do
7
+ @slices = range.slice(size)
8
+ end
9
+
10
+ it "should have #{results.length} slices" do
11
+ @slices.length.should == results.length
12
+ end
13
+
14
+ results.each_with_index do |chunk, index|
15
+ it "should have #{chunk.inspect} as slice #{index + 1}" do
16
+ @slices[index].should == chunk
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ describe Range do
23
+ describe "of 1 to 1000" do
24
+ slice_test 1..1000, 250, [ 1..250, 251..500, 501..750, 751..1000 ]
25
+ slice_test 1..1000, 333, [ 1..333, 334..666, 667..999, 1000..1000 ]
26
+ slice_test 1..1000, 1000, [ 1..1000 ]
27
+ slice_test 1..1000, 10_000, [ 1..1000 ]
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: range_slice
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2008-04-17 00:00:00 -07:00
8
+ summary: Adds Range#slice
9
+ require_paths:
10
+ - lib
11
+ email: mikelovesrobots@gmail.com
12
+ homepage: " by Mike Judge"
13
+ rubyforge_project: rubysideshow
14
+ description: "== SYNOPSIS: (1..1000).slice(300) # => [1..300, 301..600, 601..900, 901..1000] require 'active_support' (7.days.ago..Time.now).slice(24.hours) # => [thu apr 10 17:48:08 -0700 2008..fri apr 11 17:48:07 -0700 2008, fri apr 11 17:48:08 -0700 2008..sat apr 12 17:48:07 -0700 2008, sat apr 12 17:48:08 -0700 2008..sun apr 13 17:48:07 -0700 2008, sun apr 13 17:48:08 -0700 2008..mon apr 14 17:48:07 -0700 2008, mon apr 14 17:48:08 -0700 2008..tue apr 15 17:48:07 -0700 2008, tue apr 15 17:48:08 -0700 2008..wed apr 16 17:48:07 -0700 2008, wed apr 16 17:48:08 -0700 2008..thu apr 17 17:48:07 -0700 2008, thu apr 17 17:48:08 -0700 2008..thu apr 17 17:48:08 -0700 2008] == INSTALL: * sudo gem install range_slice == LICENSE:"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ - |
29
+ -----BEGIN CERTIFICATE-----
30
+ MIIDQDCCAiigAwIBAgIBADANBgkqhkiG9w0BAQUFADBGMRgwFgYDVQQDDA9taWtl
31
+ bG92ZXNyb2JvdH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EtfjlCfufMvcp
47
+ aHG2UbipyooL1m/rJWEPIllGmbk=
48
+ -----END CERTIFICATE-----
49
+
50
+ post_install_message:
51
+ authors:
52
+ - Mike Judge
53
+ files:
54
+ - History.txt
55
+ - Manifest.txt
56
+ - README.txt
57
+ - Rakefile
58
+ - lib/range_slice.rb
59
+ - test/range_slice_spec.rb
60
+ test_files: []
61
+
62
+ rdoc_options:
63
+ - --main
64
+ - README.txt
65
+ extra_rdoc_files:
66
+ - History.txt
67
+ - Manifest.txt
68
+ - README.txt
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ requirements: []
74
+
75
+ dependencies:
76
+ - !ruby/object:Gem::Dependency
77
+ name: hoe
78
+ version_requirement:
79
+ version_requirements: !ruby/object:Gem::Version::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 1.5.1
84
+ version:
Binary file