Last active
July 13, 2017 04:25
-
-
Save vtno/f3b864b5567de65b0f35c5802619409f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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