Module: CovidPT::Utils
Instance Method Summary collapse
Instance Method Details
#cumulative_diff(a, b, start) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/covid_pt/utils.rb', line 15 def cumulative_diff(a, b, start) a.inject(start) do |acc, (key, value)| b_value = b[key] || 0 a_value = value || 0 acc[key] ||= {values: [a_value], differences: []} new_entries = {values: [b_value], differences: [b_value - a_value]} acc[key].merge(new_entries) { |_, old, new| old.concat(new) } acc end end |
#diff(a, b) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/covid_pt/utils.rb', line 5 def diff(a, b) a.inject({}) do |acc, (k, v)| b_value = b[k] || 0 a_value = v || 0 acc[k] = { value: v, difference: a_value - b_value } acc end end |