Skip to content

Instantly share code, notes, and snippets.

@vtno
Last active July 13, 2017 04:25
Show Gist options
  • Save vtno/f3b864b5567de65b0f35c5802619409f to your computer and use it in GitHub Desktop.
Save vtno/f3b864b5567de65b0f35c5802619409f to your computer and use it in GitHub Desktop.
def weight_avg_gross_profit(reports)
weight_avg(reports, 'gross_profit')
end
def weight_avg_cogs(reports)
weight_avg(reports, 'cogs')
end
def weight_avg(reports, value)
total_weight = reports.reduce(0) { |sum, r| sum + r.sale_revenue }
sum_prod = reports.reduce(0) do |sum, res|
sum + (res.send(value) * res.sale_revenue)
end
sum_prod / total_weight
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment