Class: FinancialMath::GeometricProgression
- Inherits:
-
Object
- Object
- FinancialMath::GeometricProgression
- Defined in:
- lib/financial_math/geometric_progression.rb
Instance Attribute Summary collapse
-
#initial_value ⇒ Object
readonly
Returns the value of attribute initial_value.
-
#ratio ⇒ Object
readonly
Returns the value of attribute ratio.
-
#times ⇒ Object
readonly
Returns the value of attribute times.
Instance Method Summary collapse
- #infinite_decreasing_sum ⇒ Object
-
#initialize(args) ⇒ GeometricProgression
constructor
A new instance of GeometricProgression.
- #last_item ⇒ Object
- #sum ⇒ Object
Constructor Details
#initialize(args) ⇒ GeometricProgression
Returns a new instance of GeometricProgression.
5 6 7 8 9 |
# File 'lib/financial_math/geometric_progression.rb', line 5 def initialize(args) @initial_value = args.fetch(:initial_value, 1) @ratio = args.fetch(:ratio, 0) @times = args.fetch(:times, 1) end |
Instance Attribute Details
#initial_value ⇒ Object (readonly)
Returns the value of attribute initial_value.
3 4 5 |
# File 'lib/financial_math/geometric_progression.rb', line 3 def initial_value @initial_value end |
#ratio ⇒ Object (readonly)
Returns the value of attribute ratio.
3 4 5 |
# File 'lib/financial_math/geometric_progression.rb', line 3 def ratio @ratio end |
#times ⇒ Object (readonly)
Returns the value of attribute times.
3 4 5 |
# File 'lib/financial_math/geometric_progression.rb', line 3 def times @times end |
Instance Method Details
#infinite_decreasing_sum ⇒ Object
25 26 27 |
# File 'lib/financial_math/geometric_progression.rb', line 25 def infinite_decreasing_sum initial_value / (1 - ratio) end |
#last_item ⇒ Object
11 12 13 |
# File 'lib/financial_math/geometric_progression.rb', line 11 def last_item (initial_value * ratio**(times - 1)).round(2) end |
#sum ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/financial_math/geometric_progression.rb', line 15 def sum if ratio > 1 growing_progression elsif ratio < 1 decreasing_progression elsif ratio == 1 raise ZeroDivisionError, "ratio can't be equal to one" end end |