From 412118c99c63f62224a7301042c675d155b5efc0 Mon Sep 17 00:00:00 2001 From: tubedude Date: Thu, 8 May 2014 22:22:55 -0300 Subject: [PATCH] Added an intermediary variable in nvp formula This will prevent npv from changing the given array content. --- lib/finance/cashflows.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/finance/cashflows.rb b/lib/finance/cashflows.rb index 592e89f..2eaba5b 100644 --- a/lib/finance/cashflows.rb +++ b/lib/finance/cashflows.rb @@ -70,10 +70,10 @@ def method_missing(name, *args, &block) # @see http://en.wikipedia.org/wiki/Net_present_value # @api public def npv(rate) - self.collect! { |entry| Flt::DecNum.new(entry.to_s) } + cashflow = self.collect { |entry| Flt::DecNum.new(entry.to_s) } rate, total = Flt::DecNum.new(rate.to_s), Flt::DecNum.new(0.to_s) - self.each_with_index do |cashflow, index| + cashflow.each_with_index do |cashflow, index| total += cashflow / (1 + rate) ** index end