nutella 0.3 → 0.4

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.
@@ -40,7 +40,8 @@ module Enumerable
40
40
  self.replace group(size, discard_excess)
41
41
  end
42
42
 
43
- # Add together all numeric elements in the collection.
43
+ # Add together all numeric elements in the collection. When using a block,
44
+ # adds together all numeric elements that pass the predicate in that block.
44
45
  #
45
46
  # @example Sums up the elements of an array
46
47
  # [1, 2, 3].sum # => 6
@@ -50,9 +51,15 @@ module Enumerable
50
51
  # { a: 4, b: 5 }.sum # => 9
51
52
  # @example Ignores the non-numeric element in the array
52
53
  # [2, "string", 9].sum # => 11
54
+ # @example Sums up the even numbers from 1 to 10
55
+ # (1..10).sum &:even?
56
+ # @example Sums up the numbers from 1 to 100 that are multiples of 3
57
+ # (1..100).sum { |n| n % 3 == 0 }
53
58
  #
54
59
  # @return [Numeric] the sum of all numeric elements in the collection
55
- def sum
56
- flatten.select { |e| e.is_a? Numeric }.inject(:+) || 0
60
+ def sum(&block)
61
+ # TODO: Two selects in a row, how to DRY this up?
62
+ flat = respond_to?(:flatten) ? flatten : self
63
+ flat.select { |e| e.is_a? Numeric }.select(&block).inject(:+) || 0
57
64
  end
58
65
  end
@@ -1,5 +1,36 @@
1
1
  class Integer
2
- # Check whether the integer is evenly divisible by all of the arguments.
2
+ # Checks whether the integer goes evenly into all of the arguments
3
+ #
4
+ # @example Check if 3 goes into some numbers
5
+ # 3.goes_into? 10 # => false
6
+ # 3.goes_into? 6 # => true
7
+ #
8
+ # @example Check if 5 goes into every number in some sets of numbers
9
+ # 5.goes_into? 10, 12 # => false
10
+ # 5.goes_into? 10, 20 # => true
11
+ #
12
+ # @param [*Integer] nums the integer(s) to check against
13
+ # @return [Boolean] whether or not the integer goes evenly into all of the
14
+ # arguments
15
+ def goes_into?(*nums)
16
+ nums.all? { |n| (!zero? && n % self == 0) || n.zero? }
17
+ end
18
+
19
+ # Checks whether the integer goes evenly into any of the arguments
20
+ #
21
+ # @example Check if 5 goes into every number in some sets of numbers
22
+ # 5.goes_into_any? 10, 20 # => true
23
+ # 5.goes_into_any? 10, 12 # => true
24
+ # 5.goes_into_any? 12, 21 # => false
25
+ #
26
+ # @param [*Integer] nums the integer(s) to check against
27
+ # @return [Boolean] whether or not the integer goes evenly into any of the
28
+ # arguments
29
+ def goes_into_any?(*nums)
30
+ nums.any? { |n| goes_into? n }
31
+ end
32
+
33
+ # Checks whether the integer is evenly divisible by all of the arguments.
3
34
  #
4
35
  # @example Check if some numbers are evenly divisible by 5
5
36
  # 4.multiple_of? 5 # => false
@@ -17,10 +48,10 @@ class Integer
17
48
  end
18
49
  alias_method :divisible_by?, :multiple_of?
19
50
 
20
- # Check whether the integer is evenly divisible by any of the arguments.
51
+ # Checks whether the integer is evenly divisible by any of the arguments.
21
52
  #
22
53
  # @example {Project Euler #1}[http://projecteuler.net/problem=1] solution using Nutella
23
- # (1...1000).select { |n| n.multiple_of_any?(3, 5) }.sum
54
+ # (1...1000).sum { |n| n.multiple_of_any?(3, 5) }
24
55
  #
25
56
  # @param [*Integer] nums the integer(s) to check against
26
57
  # @return [Boolean] whether or not the integer is evenly divisible by any
@@ -1,4 +1,4 @@
1
1
  module Nutella
2
2
  # The current version of Nutella.
3
- VERSION = "0.3"
3
+ VERSION = "0.4"
4
4
  end
@@ -73,6 +73,7 @@ describe Enumerable do
73
73
  [].sum.should == 0
74
74
  [5].sum.should == 5
75
75
  [1, 2, 3].sum.should == 6
76
+ (1..4).sum.should == 10
76
77
  end
77
78
 
78
79
  it "should flatten the elements before adding" do
@@ -85,5 +86,17 @@ describe Enumerable do
85
86
  [1, "str", 4].sum.should == 5
86
87
  ["no numbers"].sum.should == 0
87
88
  end
89
+
90
+ context "with a block" do
91
+ it "should return the sum of the elements that pass the filter block" do
92
+ (1..10).sum(&:even?).should == 30
93
+ (1..10).sum { |n| n % 3 == 0 }.should == 18
94
+ end
95
+
96
+ it "should still filter out all non-numbers" do
97
+ { a: 1, b: 2, c: 3 }.sum(&:odd?).should == 4
98
+ [2, "str", 4, 7].sum(&:even?).should == 6
99
+ end
100
+ end
88
101
  end
89
102
  end
@@ -7,6 +7,65 @@ describe Integer do
7
7
  test_alias Integer, :divisible_by_any?, :multiple_of_any?
8
8
  end
9
9
 
10
+ describe "#goes_into?" do
11
+ it "should return true if the number goes evenly into the argument" do
12
+ 5.goes_into?(10).should be_true
13
+ 3.goes_into?(21).should be_true
14
+ 25.goes_into?(100).should be_true
15
+ end
16
+
17
+ it "should return false if the number does not go evenly in" do
18
+ 3.goes_into?(10).should be_false
19
+ 9.goes_into?(40).should be_false
20
+ 10.goes_into?(5).should be_false
21
+ end
22
+
23
+ context "when passing in zero" do
24
+ it "should return false if one tries to divide by zero" do
25
+ 0.goes_into?(20).should be_false
26
+ 0.goes_into?(30).should be_false
27
+ end
28
+
29
+ it "should allow zero to go into zero" do
30
+ 0.goes_into?(0).should be_true
31
+ end
32
+ end
33
+
34
+ context "with multiple arguments" do
35
+ it "should return true if all arguments succeed" do
36
+ 5.goes_into?(10, 15, 50).should be_true
37
+ 2.goes_into?(2, 4, 10).should be_true
38
+ end
39
+
40
+ it "should return false if only some arguments succeed" do
41
+ 5.goes_into?(10, 12, 15).should be_false
42
+ 8.goes_into?(4, 16).should be_false
43
+ end
44
+
45
+ it "should return false if no arguments succeed" do
46
+ 3.goes_into?(8, 16, 20).should be_false
47
+ 6.goes_into?(5, 10, 15).should be_false
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#goes_into_any?" do
53
+ it "should return true if all arguments succeed" do
54
+ 5.goes_into_any?(10, 15, 50).should be_true
55
+ 2.goes_into_any?(2, 4, 10).should be_true
56
+ end
57
+
58
+ it "should return true if only some arguments succeed" do
59
+ 5.goes_into_any?(10, 12, 15).should be_true
60
+ 8.goes_into_any?(4, 16).should be_true
61
+ end
62
+
63
+ it "should return false if no arguments succeed" do
64
+ 3.goes_into_any?(8, 16, 20).should be_false
65
+ 6.goes_into_any?(5, 10, 15).should be_false
66
+ end
67
+ end
68
+
10
69
  describe "#multiple_of?" do
11
70
  it "should return true if the number is evenly divisible" do
12
71
  5.multiple_of?(5).should be_true
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.3'
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  segments:
137
137
  - 0
138
- hash: 1233150412608386870
138
+ hash: -2685001604496901448
139
139
  requirements: []
140
140
  rubyforge_project:
141
141
  rubygems_version: 1.8.24