visionmedia-rext 0.0.7 → 0.0.8
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.
- data/History.rdoc +5 -0
- data/lib/rext/array/helpers.rb +46 -0
- data/lib/rext/version.rb +1 -1
- data/rext.gemspec +2 -2
- data/spec/array_spec.rb +36 -0
- metadata +2 -2
data/History.rdoc
CHANGED
data/lib/rext/array/helpers.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
|
2
|
+
require 'enumerator'
|
3
|
+
|
2
4
|
class Array
|
3
5
|
|
4
6
|
##
|
@@ -15,4 +17,48 @@ class Array
|
|
15
17
|
self[0..position]
|
16
18
|
end
|
17
19
|
|
20
|
+
##
|
21
|
+
# Pad array with expected length +n+, and +pad_with+ an
|
22
|
+
# optional object or nil.
|
23
|
+
#
|
24
|
+
# === Examples
|
25
|
+
#
|
26
|
+
# [1,2].pad(4) # => [1,2,nil,nil]
|
27
|
+
# [1,2].pad(4) # => [1,2,'x','x']
|
28
|
+
# [1,2].pad(2) # => [1,2]
|
29
|
+
#
|
30
|
+
|
31
|
+
def pad n, pad_with = nil
|
32
|
+
fill pad_with, length, n - length
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Split an array into chunks of length +n+. Optionally you
|
37
|
+
# may +pad_with+ an object to retain a uniform length per chunk.
|
38
|
+
#
|
39
|
+
# === Examples
|
40
|
+
#
|
41
|
+
# [1,2,3].chunk(2) # => [[1,2], [3, nil]]
|
42
|
+
# [1,2,3].chunk(2, 'x') # => [[1,2], [3, 'x']]
|
43
|
+
# [1,2,3].chunk(2, false) # => [[1,2], [3]]
|
44
|
+
# [1,2,3].in_groups_of(2) do |chunk|
|
45
|
+
# # Do something
|
46
|
+
# end
|
47
|
+
#
|
48
|
+
# === See
|
49
|
+
#
|
50
|
+
# * Array#pad
|
51
|
+
#
|
52
|
+
|
53
|
+
def chunk n, pad_with = nil, &block
|
54
|
+
chunks = []
|
55
|
+
each_slice n do |chunk|
|
56
|
+
chunk.pad n, pad_with unless pad_with == false
|
57
|
+
yield chunk if block
|
58
|
+
chunks << chunk
|
59
|
+
end
|
60
|
+
chunks
|
61
|
+
end
|
62
|
+
alias :in_groups_of :chunk
|
63
|
+
|
18
64
|
end
|
data/lib/rext/version.rb
CHANGED
data/rext.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{rext}
|
5
|
-
s.version = "0.0.
|
5
|
+
s.version = "0.0.8"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["TJ Holowaychuk"]
|
9
|
-
s.date = %q{2009-04-
|
9
|
+
s.date = %q{2009-04-13}
|
10
10
|
s.description = %q{Ruby extensions}
|
11
11
|
s.email = %q{tj@vision-media.ca}
|
12
12
|
s.extra_rdoc_files = ["lib/rext/all.rb", "lib/rext/array/helpers.rb", "lib/rext/array.rb", "lib/rext/compat/basic_object.rb", "lib/rext/compat.rb", "lib/rext/date/helpers.rb", "lib/rext/date.rb", "lib/rext/enumerable/helpers.rb", "lib/rext/enumerable.rb", "lib/rext/hash/helpers.rb", "lib/rext/hash.rb", "lib/rext/integer/helpers.rb", "lib/rext/integer.rb", "lib/rext/module/helpers.rb", "lib/rext/module.rb", "lib/rext/numeric/bytes.rb", "lib/rext/numeric/time.rb", "lib/rext/numeric.rb", "lib/rext/object/helpers.rb", "lib/rext/object/metaclass.rb", "lib/rext/object.rb", "lib/rext/proc/helpers.rb", "lib/rext/proc.rb", "lib/rext/string/escape.rb", "lib/rext/string/helpers.rb", "lib/rext/string.rb", "lib/rext/time/helpers.rb", "lib/rext/time.rb", "lib/rext/version.rb", "lib/rext.rb", "README.rdoc", "tasks/benchmark.rake", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
|
data/spec/array_spec.rb
CHANGED
@@ -20,5 +20,41 @@ describe Array do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
+
describe "#chunk" do
|
24
|
+
it "should split an array into several containing the length of n" do
|
25
|
+
(1..9).to_a.chunk(3).should == [[1,2,3], [4,5,6], [7,8,9]]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should accept a block, yielding each chunk" do
|
29
|
+
chunks = []
|
30
|
+
(1..9).to_a.in_groups_of(3) do |chunk|
|
31
|
+
chunks << chunk
|
32
|
+
end
|
33
|
+
chunks.should == [[1,2,3], [4,5,6], [7,8,9]]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should pad with nil by default" do
|
37
|
+
(1..5).to_a.in_groups_of(4).should == [[1,2,3,4], [5,nil,nil,nil]]
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not padd when pad_with is false" do
|
41
|
+
(1..6).to_a.in_groups_of(4, false).should == [[1,2,3,4], [5,6]]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#pad" do
|
46
|
+
it "should pad with nil by default" do
|
47
|
+
[1,2].pad(4).should == [1, 2, nil, nil]
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should pad an with an object" do
|
51
|
+
[1,2].pad(3 ,'x').should == [1, 2, 'x']
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should do nothing when the array is of expected length" do
|
55
|
+
[1,2].pad(2).should == [1,2]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
23
59
|
end
|
24
60
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: visionmedia-rext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TJ Holowaychuk
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-13 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|