Skip to content

Instantly share code, notes, and snippets.

@vtno
Last active July 13, 2017 04:24
Show Gist options
  • Save vtno/150f9cfe337eab54bcf3b1c864541e15 to your computer and use it in GitHub Desktop.
Save vtno/150f9cfe337eab54bcf3b1c864541e15 to your computer and use it in GitHub Desktop.
Refactoring blog: Parameterization
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