numeric_hash 0.4.0 → 0.5.0
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.
- data/lib/numeric_hash/version.rb +1 -1
- data/lib/numeric_hash.rb +15 -2
- metadata +4 -4
data/lib/numeric_hash/version.rb
CHANGED
data/lib/numeric_hash.rb
CHANGED
@@ -75,11 +75,15 @@ class NumericHash < ::Hash
|
|
75
75
|
# @hash.normalize(120) # => { :a => 12.0, :b => 24.0, :c => 36.0, :d => 48.0 }
|
76
76
|
#
|
77
77
|
def normalize(magnitude = 1.0)
|
78
|
-
norm_factor = magnitude
|
79
|
-
norm_factor = 0.0 unless norm_factor.finite? # If total was zero, the normalization factor will not be finite; set it to zero in this case.
|
78
|
+
norm_factor = normalization_factor(magnitude)
|
80
79
|
map_values { |value| value * norm_factor }
|
81
80
|
end
|
82
81
|
|
82
|
+
def normalize!(magnitude = 1.0)
|
83
|
+
norm_factor = normalization_factor(magnitude)
|
84
|
+
map_values! { |value| value * norm_factor }
|
85
|
+
end
|
86
|
+
|
83
87
|
# Shortcuts to normalize the hash to various totals.
|
84
88
|
#
|
85
89
|
def to_ratio
|
@@ -407,6 +411,15 @@ protected
|
|
407
411
|
self
|
408
412
|
end
|
409
413
|
|
414
|
+
# Helper method for calculating the normalization factor that needs to be
|
415
|
+
# applied to each value for a given magnitude.
|
416
|
+
#
|
417
|
+
def normalization_factor(magnitude)
|
418
|
+
norm_factor = magnitude / total.to_f
|
419
|
+
norm_factor = 0.0 unless norm_factor.finite? # If total was zero, the normalization factor will not be finite; set it to zero in this case.
|
420
|
+
norm_factor
|
421
|
+
end
|
422
|
+
|
410
423
|
class << self
|
411
424
|
|
412
425
|
# Sums an array of NumericHashes, taking into account empty arrays.
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: numeric_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 5
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Clyde Law
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-03-05 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|