darthjee-core_ext 1.5.3 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b584506156a0622988d8fe4b00681f318ebc9660
4
- data.tar.gz: 3f554744e539f3440408f9d971bdd45b5dbf24f5
3
+ metadata.gz: b2c5c7d0ee2381eb9ee8247dcf6958d094cc5048
4
+ data.tar.gz: c2edd6e256d0af3754a452dd1866ffab79e54c2b
5
5
  SHA512:
6
- metadata.gz: 428fc4e08889758330f638d25299cb5b26aee30cf284b3e7fcf548872e972e59dc27f61a69d753ec845e32ae4be0a17c0134ffcc3b7629632d0ceb91caf67d1a
7
- data.tar.gz: b2fd1bac68f74aeabe0a4e0095c0d1db5a5364e0ea36cb3929c79f9633f110cfb465965fb01f661f85ec0879da0a02fd3d6805acc293bfff836922ff69538ca7
6
+ metadata.gz: a563d810e044f19c03347289e619c8ab3fae73572a475b14996009714ce136fcd50fd6586d814ef6b417d247f15d977be2b3c5d64545a2a15ec56548fd246fe9
7
+ data.tar.gz: 894aad77a3355ed2cd43104de005088acce770872bef017e0467363b4b66c9eb119bf2e5216661ae1ee911bb4f9e52fc4207d382a99573f4c971dbd6ba049d0b
data/MATH_README.md ADDED
@@ -0,0 +1,30 @@
1
+ ## MATH
2
+
3
+ ### .average
4
+ calculates the average of the given values
5
+
6
+ ```ruby
7
+ Math.average([ 0, 1, 2, 3, 4 ])
8
+ ```
9
+
10
+ returns
11
+ ```
12
+ 2
13
+ ```
14
+
15
+ It can also be used to calculate the average of weighted values
16
+
17
+ ```ruby
18
+ Math.average(
19
+ 0 => 1,
20
+ 1 => 2,
21
+ 2 => 3,
22
+ 3 => 4,
23
+ 4 => 5
24
+ )
25
+ ```
26
+
27
+ returns
28
+ ```
29
+ 2.6666
30
+ ```
data/README.md CHANGED
@@ -38,3 +38,5 @@ bundle install darthjee-core_ext
38
38
  [DATE_README.md](DATE_README.md)
39
39
  ## Object
40
40
  [OBJECT_README.md](OBJECT_README.md)
41
+ ## Math
42
+ [MATH_README.md](MATH_README.md)
@@ -7,6 +7,7 @@ require 'darthjee/core_ext/numeric'
7
7
  require 'darthjee/core_ext/date'
8
8
  require 'darthjee/core_ext/time'
9
9
  require 'darthjee/core_ext/object'
10
+ require 'darthjee/core_ext/math'
10
11
 
11
12
  module Darthjee
12
13
  module CoreExt
@@ -0,0 +1,9 @@
1
+ module Math
2
+ def self.average(values)
3
+ values = values.map { |v| [v,1] }.to_h unless values.is_a?(Hash)
4
+
5
+ values.inject(0) do |sum, vals|
6
+ sum + vals.inject { |a,b| a * b }
7
+ end / values.values.sum.to_f
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Darthjee
2
2
  module CoreExt
3
- VERSION = '1.5.3'
3
+ VERSION = '1.5.4'
4
4
  end
5
5
  end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Math do
4
+ describe '.average' do
5
+ context 'when passing an array of values' do
6
+ let(:values) { [0, 1, 2, 3, 4] }
7
+
8
+ it 'returns the average' do
9
+ expect(described_class.average(values)).to eq(2)
10
+ end
11
+
12
+ context 'when average is not a round number' do
13
+ let(:values) { [0, 1, 2, 3] }
14
+ it 'returns the average' do
15
+ expect(described_class.average(values)).to eq(1.5)
16
+ end
17
+ end
18
+ end
19
+
20
+ context 'when passing a hash' do
21
+ let(:values) do
22
+ {
23
+ 0 => 1,
24
+ 1 => 2,
25
+ 2 => 3,
26
+ 3 => 4,
27
+ 4 => 5
28
+ }
29
+ end
30
+
31
+ it 'uses the keys as values and the values as weights (frequency)' do
32
+ expect(described_class.average(values)).to eq(8 / 3.0)
33
+ end
34
+ end
35
+ end
36
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: darthjee-core_ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.3
4
+ version: 1.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-05 00:00:00.000000000 Z
11
+ date: 2018-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -137,6 +137,7 @@ files:
137
137
  - Gemfile
138
138
  - HASH_README.md
139
139
  - LICENSE
140
+ - MATH_README.md
140
141
  - OBJECT_README.md
141
142
  - README.md
142
143
  - Rakefile
@@ -154,6 +155,7 @@ files:
154
155
  - lib/darthjee/core_ext/hash/deep_hash_constructor.rb
155
156
  - lib/darthjee/core_ext/hash/key_changer.rb
156
157
  - lib/darthjee/core_ext/hash/value_changer.rb
158
+ - lib/darthjee/core_ext/math.rb
157
159
  - lib/darthjee/core_ext/numeric.rb
158
160
  - lib/darthjee/core_ext/object.rb
159
161
  - lib/darthjee/core_ext/object/default_value.rb
@@ -166,6 +168,7 @@ files:
166
168
  - spec/lib/hash/deep_hash_constructor_spec.rb
167
169
  - spec/lib/hash/key_changer_spec.rb
168
170
  - spec/lib/hash_spec.rb
171
+ - spec/lib/math_spec.rb
169
172
  - spec/lib/numeric_spec.rb
170
173
  - spec/lib/object/default_value_spe.rb
171
174
  - spec/lib/object_spec.rb
@@ -219,6 +222,7 @@ test_files:
219
222
  - spec/lib/hash/deep_hash_constructor_spec.rb
220
223
  - spec/lib/hash/key_changer_spec.rb
221
224
  - spec/lib/hash_spec.rb
225
+ - spec/lib/math_spec.rb
222
226
  - spec/lib/numeric_spec.rb
223
227
  - spec/lib/object/default_value_spe.rb
224
228
  - spec/lib/object_spec.rb