Module: TrickBag::Numeric::Totals
- Defined in:
- lib/trick_bag/numeric/totals.rb
Class Method Summary collapse
-
.fraction_of_total_hash(the_hash) ⇒ Object
Given a hash whose values are numbers, produces a new hash with the same keys as the original hash, but whose values are the % of total.
-
.map_fraction_of_total(inputs) ⇒ Object
e.g.: map_fraction_of_total [10,20,30] => [0.16666666666666666, 0.3333333333333333, 0.5].
-
.map_percent_of_total(inputs) ⇒ Object
e.g.
-
.percent_of_total_hash(the_hash) ⇒ Object
Given a hash whose values are numbers, produces a new hash with the same keys as the original hash, but whose values are the % of total.
Class Method Details
.fraction_of_total_hash(the_hash) ⇒ Object
Given a hash whose values are numbers, produces a new hash with the same keys as the original hash, but whose values are the % of total.
Ex: fraction_of_total_hash({ foo: 10.0, bar: 20.0, baz: 30.0, razz: 40.0 })
=> {:foo=>0.1, :bar=>0.2, :baz=>0.3, :razz=>0.4}
32 33 34 35 36 37 38 |
# File 'lib/trick_bag/numeric/totals.rb', line 32 def fraction_of_total_hash(the_hash) new_hash = percent_of_total_hash(the_hash) new_hash.keys.each do |key| new_hash[key] = new_hash[key] / 100.0 end new_hash end |
.map_fraction_of_total(inputs) ⇒ Object
e.g.: map_fraction_of_total [10,20,30] => [0.16666666666666666, 0.3333333333333333, 0.5]
11 12 13 14 15 |
# File 'lib/trick_bag/numeric/totals.rb', line 11 def map_fraction_of_total(inputs) return [] if inputs.size == 0 sum = Float(inputs.inject(:+)) inputs.map { |n| n / sum } end |
.map_percent_of_total(inputs) ⇒ Object
e.g. map_percent_of_total [10,20,30] => [16.666666666666664, 33.33333333333333, 50.0]
22 23 24 |
# File 'lib/trick_bag/numeric/totals.rb', line 22 def map_percent_of_total(inputs) map_fraction_of_total(inputs).map { |n| n * 100 } end |
.percent_of_total_hash(the_hash) ⇒ Object
Given a hash whose values are numbers, produces a new hash with the same keys as the original hash, but whose values are the % of total.
Ex: percent_of_total_hash({ foo: 10.0, bar: 20.0, baz: 30.0, razz: 40.0 })
=> {:foo=>10.0, :bar=>20.0, :baz=>30.0, :razz=>40.0}
46 47 48 49 50 51 52 |
# File 'lib/trick_bag/numeric/totals.rb', line 46 def percent_of_total_hash(the_hash) sum = Float(the_hash.values.inject(:+)) keys = the_hash.keys keys.each_with_object({}) do |key, percent_total_hash| percent_total_hash[key] = 100 * the_hash[key] / sum end end |