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 +10 -0
- data/VERSION +1 -1
- data/lib/numeric_array.rb +7 -4
- data/spec/numeric_array_spec.rb +3 -4
- metadata +2 -2
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.
|
1
|
+
1.0.1
|
data/lib/numeric_array.rb
CHANGED
@@ -3,10 +3,13 @@ module NumericArray
|
|
3
3
|
|
4
4
|
NUMERIC_REGEX = /^-?\d+.?\d*e?-?\d*$/
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
data/spec/numeric_array_spec.rb
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe NumericArray do
|
4
|
-
describe [
|
5
|
-
let(:array) { [
|
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
|