nutella 0.0.1 → 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/README.md +12 -0
- data/lib/nutella/core_ext/enumerable.rb +20 -0
- data/lib/nutella/core_ext/string.rb +1 -1
- data/lib/nutella/version.rb +1 -1
- data/nutella.gemspec +1 -1
- data/spec/nutella/core_ext/enumerable_spec.rb +50 -0
- metadata +3 -2
data/.rspec
ADDED
data/README.md
CHANGED
@@ -13,10 +13,21 @@ Install with RubyGems:
|
|
13
13
|
gem install nutella
|
14
14
|
```
|
15
15
|
|
16
|
+
## Contributing
|
17
|
+
|
18
|
+
This library is my own little collection of core extensions that I like to use,
|
19
|
+
and I add things as they are useful to me. I cannot guarantee that you will
|
20
|
+
find it useful as I do.
|
21
|
+
|
22
|
+
That being said, I welcome any contributions. Feel free to send a pull request
|
23
|
+
if you feel that you have a feature that is useful, and definitely let me know
|
24
|
+
if you find any bugs.
|
25
|
+
|
16
26
|
## License
|
17
27
|
|
18
28
|
```nutella``` is released under the MIT license.
|
19
29
|
|
30
|
+
```
|
20
31
|
Copyright (C) 2012 Vinny Diehl
|
21
32
|
|
22
33
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
@@ -36,6 +47,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
36
47
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
37
48
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
38
49
|
SOFTWARE.
|
50
|
+
```
|
39
51
|
|
40
52
|
See [the official page on OSI](http://opensource.org/licenses/MIT) for more
|
41
53
|
information.
|
@@ -9,6 +9,26 @@ module Enumerable
|
|
9
9
|
end
|
10
10
|
alias_method :excludes?, :exclude?
|
11
11
|
|
12
|
+
# Groups an array into smaller arrays of +size+ elements. Remaining elements
|
13
|
+
# at the end will be put into a smaller group if necessary, unless
|
14
|
+
# +discard_excess+ is true, in which case they will be discarded.
|
15
|
+
#
|
16
|
+
# (1..4).group(2) # => [[1, 2], [3, 4]]
|
17
|
+
# (1..8).group(3) # => [[1, 2, 3], [4, 5, 6], [7, 8]]
|
18
|
+
# (1..8).group(3, true) # => [[1, 2, 3], [4, 5, 6]]
|
19
|
+
def group(size, discard_excess = false)
|
20
|
+
groups = each_slice(size).to_a
|
21
|
+
discard_excess && groups.last.size < size ? groups[0..-2] : groups
|
22
|
+
end
|
23
|
+
|
24
|
+
# Modifies the collection in place as described for
|
25
|
+
# <tt>Enumerable#group</tt>. Returns the modified collection, or nil if no
|
26
|
+
# modifications were made.
|
27
|
+
def group!(size, discard_excess = false)
|
28
|
+
return nil if empty?
|
29
|
+
self.replace group(size, discard_excess)
|
30
|
+
end
|
31
|
+
|
12
32
|
# Returns the sum of all numeric elements in the collection
|
13
33
|
#
|
14
34
|
# [1, 2, 3].sum # => 6
|
data/lib/nutella/version.rb
CHANGED
data/nutella.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.require_paths = %w[lib]
|
17
17
|
gem.test_files = Dir["spec/**/*"]
|
18
18
|
gem.files = Dir["lib/**/*"] + gem.test_files + %w[
|
19
|
-
LICENSE Rakefile README.md nutella.gemspec
|
19
|
+
.rspec LICENSE Rakefile README.md nutella.gemspec
|
20
20
|
]
|
21
21
|
|
22
22
|
gem.required_ruby_version = ">= 1.9.2"
|
@@ -18,6 +18,56 @@ describe Enumerable do
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
describe "#group" do
|
22
|
+
it "should group elements" do
|
23
|
+
[].group(2).should == []
|
24
|
+
[1, 2].group(2).should == [[1, 2]]
|
25
|
+
(1..4).group(2).should == [[1, 2], [3, 4]]
|
26
|
+
(1..9).group(3).should == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should put all excess into the last group" do
|
30
|
+
[1, 2].group(4).should == [[1, 2]]
|
31
|
+
(1..8).group(3).should == [[1, 2, 3], [4, 5, 6], [7, 8]]
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should discard all excess if instructed to" do
|
35
|
+
(1..3).group(2, true).should == [[1, 2]]
|
36
|
+
(1..8).group(3, true).should == [[1, 2, 3], [4, 5, 6]]
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not discard anything if there is no excess" do
|
40
|
+
(1..4).group(2, true).should == [[1, 2], [3, 4]]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not modify in place" do
|
44
|
+
arr = (1..10).to_a
|
45
|
+
arr.group 2
|
46
|
+
arr.should == (1..10).to_a
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#group!" do
|
51
|
+
arr = []
|
52
|
+
|
53
|
+
before { arr = (1..10).to_a }
|
54
|
+
|
55
|
+
it "should modify in place" do
|
56
|
+
arr.group! 2
|
57
|
+
arr.should == (1..10).group(2)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return the modified string" do
|
61
|
+
return_catcher = arr.group! 2
|
62
|
+
return_catcher.should == arr
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should return nil if nothing was modified" do
|
66
|
+
return_catcher = [].group! 2
|
67
|
+
return_catcher.should be_nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
21
71
|
describe "#sum" do
|
22
72
|
it "should return the sum of elements" do
|
23
73
|
[].sum.should == 0
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutella
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.1'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -57,6 +57,7 @@ files:
|
|
57
57
|
- spec/nutella/core_ext/enumerable_spec.rb
|
58
58
|
- spec/nutella/core_ext/string_spec.rb
|
59
59
|
- spec/spec_helper.rb
|
60
|
+
- .rspec
|
60
61
|
- LICENSE
|
61
62
|
- Rakefile
|
62
63
|
- README.md
|