Class: FinancialMath::GeometricProgression

Inherits:
Object
  • Object
show all
Defined in:
lib/financial_math/geometric_progression.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_valueObject (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

#ratioObject (readonly)

Returns the value of attribute ratio.



3
4
5
# File 'lib/financial_math/geometric_progression.rb', line 3

def ratio
  @ratio
end

#timesObject (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_sumObject



25
26
27
# File 'lib/financial_math/geometric_progression.rb', line 25

def infinite_decreasing_sum
  initial_value / (1 - ratio)
end

#last_itemObject



11
12
13
# File 'lib/financial_math/geometric_progression.rb', line 11

def last_item
  (initial_value * ratio**(times - 1)).round(2)
end

#sumObject



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