Last active
July 13, 2017 04:23
-
-
Save vtno/3d62ce94c670a0cb5093b5cb0123f2c4 to your computer and use it in GitHub Desktop.
Refactoring blog: Parameterized
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(0) 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 | |
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(0) do |sum, r| | |
sum + (r.cogs * 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