numeric_array 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,3 +13,13 @@ An extension for the native Array class with support for mathematical/statistica
13
13
  - Array#std_dev
14
14
 
15
15
 
16
+ Usage
17
+ -
18
+
19
+ gem install numeric_array
20
+
21
+ require 'numeric_array'
22
+
23
+ If using bundler
24
+
25
+ gem 'numeric_array', :require => 'numeric_array'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -3,10 +3,13 @@ module NumericArray
3
3
 
4
4
  NUMERIC_REGEX = /^-?\d+.?\d*e?-?\d*$/
5
5
 
6
- def sum
7
- check_numeric_array!
8
- a = numerify
9
- a.inject(0) {|sum, value| sum + value}
6
+ # Don't require numeric for this Array#sum. ActiveSupport uses it as well: ["foo", "bar"].sum #=> "foobar"
7
+ def sum(identity = 0, &block)
8
+ if block_given?
9
+ map(&block).sum(identity)
10
+ else
11
+ inject { |sum, element| sum + element } || identity
12
+ end
10
13
  end
11
14
 
12
15
  # average of an array of numbers
@@ -1,15 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe NumericArray do
4
- describe ["1", 2.52, "foo", :bar, "-1.25e45" ] do
5
- let(:array) { ["1", 2.52, "foo", :bar, "-1.25e45" ] }
4
+ describe [1, "2.52", "foo", 'bar', "-1.25e45" ] do
5
+ let(:array) { [1, "2.52", "foo", 'bar', "-1.25e45" ] }
6
6
  it { should_not be_numeric }
7
7
  it { should_not be_a_numeric_array }
8
- its(:numerify!) { lambda{ array.numerify! }.should raise_error(ArgumentError) }
9
- its(:sum) { lambda{ array.sum }.should raise_error(ArgumentError) }
10
8
  its(:avg) { lambda{ array.avg }.should raise_error(ArgumentError) }
11
9
  its(:variance) { lambda{ array.variance }.should raise_error(ArgumentError) }
12
10
  its(:std_dev) { lambda{ array.std_dev }.should raise_error(ArgumentError) }
11
+ its(:numerify!) { lambda{ array.numerify! }.should raise_error(ArgumentError) }
13
12
  end
14
13
 
15
14
  describe [1, 2.52, -8.65, 20/50.0] do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Evan Sagge