means 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/means.rb +2 -2
- data/means.gemspec +2 -2
- data/spec/means_spec.rb +45 -47
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.2.
|
1
|
+
1.2.4
|
data/lib/means.rb
CHANGED
@@ -11,7 +11,7 @@ class Mean
|
|
11
11
|
# @param [Array<Numeric>] data the data values
|
12
12
|
# @return [Numeric, nil] the arithmetic mean or nil if there is no data
|
13
13
|
def Mean.arithmetic(data)
|
14
|
-
data.
|
14
|
+
data.sum / data.size unless data.empty?
|
15
15
|
end
|
16
16
|
|
17
17
|
# Calculate the geometric mean
|
@@ -102,7 +102,7 @@ class Mean
|
|
102
102
|
end
|
103
103
|
|
104
104
|
def self.includes_zero_or_negative? data
|
105
|
-
data.any? {|element| zero_or_negative? element}
|
105
|
+
data.any? {|element| zero_or_negative? element }
|
106
106
|
end
|
107
107
|
|
108
108
|
def self.zero_or_negative? element
|
data/means.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "means"
|
8
|
-
s.version = "1.2.
|
8
|
+
s.version = "1.2.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Nigel Lowry"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2013-08-15"
|
13
13
|
s.description = "Calculates the different means for a data set (arithmetic, geometric and harmonic)."
|
14
14
|
s.email = "nigel-lowry@ultra.eclipse.co.uk"
|
15
15
|
s.extra_rdoc_files = [
|
data/spec/means_spec.rb
CHANGED
@@ -21,7 +21,7 @@ end
|
|
21
21
|
shared_examples_for "non-arithmetic means" do
|
22
22
|
[:geometric, :harmonic].each do |mean|
|
23
23
|
it "is nil when there are any zeroes" do
|
24
|
-
data = [1.1,
|
24
|
+
data = [1.1, 0.0]
|
25
25
|
Mean.send(mean, data).should be_nil
|
26
26
|
end
|
27
27
|
|
@@ -82,67 +82,65 @@ describe "Mean" do
|
|
82
82
|
end
|
83
83
|
|
84
84
|
describe "accumulators" do
|
85
|
-
|
86
|
-
@m = Mean.new()
|
87
|
-
end
|
85
|
+
let(:m) { Mean.new }
|
88
86
|
|
89
87
|
describe "#arithmetic_mean" do
|
90
88
|
it "accumulates the arithmetic mean" do
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
89
|
+
m.arithmetic_mean.should be_nil
|
90
|
+
m.push 1
|
91
|
+
m.arithmetic_mean.should == 1
|
92
|
+
m.push 2
|
93
|
+
m.arithmetic_mean.should == 1.5
|
94
|
+
m.push 3
|
95
|
+
m.arithmetic_mean.should == 2
|
96
|
+
m.push 0
|
97
|
+
m.arithmetic_mean.should == 1.5
|
98
|
+
m.push -1
|
99
|
+
m.arithmetic_mean.should == 1
|
102
100
|
end
|
103
101
|
end
|
104
102
|
|
105
103
|
describe "#geometric_mean" do
|
106
104
|
it "accumulates the geometric mean until zero" do
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
105
|
+
m.geometric_mean.should be_nil
|
106
|
+
m.push 2
|
107
|
+
m.geometric_mean.should == 2
|
108
|
+
m.push 8
|
109
|
+
m.geometric_mean.should == 4
|
110
|
+
m.push 0
|
111
|
+
m.geometric_mean.should be_nil
|
114
112
|
end
|
115
113
|
|
116
114
|
it "accumulates the geometric mean until negative" do
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
115
|
+
m.geometric_mean.should be_nil
|
116
|
+
m.push 2
|
117
|
+
m.geometric_mean.should == 2
|
118
|
+
m.push 8
|
119
|
+
m.geometric_mean.should == 4
|
120
|
+
m.push -1
|
121
|
+
m.geometric_mean.should be_nil
|
124
122
|
end
|
125
123
|
end
|
126
124
|
|
127
125
|
describe "#harmonic_mean" do
|
128
126
|
it "accumulates the harmonic mean until zero" do
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
127
|
+
m.harmonic_mean.should be_nil
|
128
|
+
m.push 1
|
129
|
+
m.harmonic_mean.should == 1
|
130
|
+
m.push 2
|
131
|
+
m.harmonic_mean.should == 4.0 / 3.0
|
132
|
+
m.push 0
|
133
|
+
m.harmonic_mean.should be_nil
|
136
134
|
end
|
137
135
|
|
138
136
|
it "accumulates the harmonic mean until negative" do
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
137
|
+
m.harmonic_mean.should be_nil
|
138
|
+
m.push 1
|
139
|
+
m.harmonic_mean.should == 1
|
140
|
+
m.push 2
|
141
|
+
m.harmonic_mean.should == 4.0 / 3.0
|
142
|
+
m.push -1
|
143
|
+
m.harmonic_mean.should be_nil
|
146
144
|
end
|
147
145
|
end
|
148
146
|
end
|
@@ -170,19 +168,19 @@ describe "Mean" do
|
|
170
168
|
end
|
171
169
|
|
172
170
|
it "rejects an unknown key" do
|
173
|
-
|
171
|
+
expect { Mean.new(count: 1, blah: 1) }.to raise_error
|
174
172
|
end
|
175
173
|
|
176
174
|
it "rejects a negative count" do
|
177
|
-
|
175
|
+
expect { Mean.new(count: -1) }.to raise_error
|
178
176
|
end
|
179
177
|
|
180
178
|
it "rejects negative sum of reciprocals" do
|
181
|
-
|
179
|
+
expect { Mean.new(sum_of_reciprocals: -1) }.to raise_error
|
182
180
|
end
|
183
181
|
|
184
182
|
it "rejects negative product" do
|
185
|
-
|
183
|
+
expect { Mean.new(product: -1) }.to raise_error
|
186
184
|
end
|
187
185
|
end
|
188
186
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: means
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.4
|
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:
|
12
|
+
date: 2013-08-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -160,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: '0'
|
161
161
|
segments:
|
162
162
|
- 0
|
163
|
-
hash:
|
163
|
+
hash: 846031021487915499
|
164
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
165
|
none: false
|
166
166
|
requirements:
|