Last active
July 13, 2017 04:24
-
-
Save vtno/150f9cfe337eab54bcf3b1c864541e15 to your computer and use it in GitHub Desktop.
Refactoring blog: Parameterization
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) | |
# find total weight, sum of sale_revenue | |
total_weight = reports.reduce(0) { |sum, r| sum + r.sale_revenue } | |
# find sum products | |
sum_prod = reports.reduce do |sum, r| | |
sum + (r.percent_gross_profit * r.sale_revenue) | |
end | |
# simple weight average formula, (w1*x1 + w2*x2) / (w1 + w2) | |
sum_prod / total_weight | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment