Class: Finance::Rate
- Inherits:
-
Object
- Object
- Finance::Rate
- Includes:
- Comparable
- Defined in:
- lib/finance/rates.rb
Overview
the Rate class provides an interface for working with interest rates.
Constant Summary collapse
- TYPES =
Accepted rate types
{ :apr => "effective", :apy => "effective", :effective => "effective", :nominal => "nominal" }
Instance Attribute Summary collapse
-
#duration ⇒ Integer
The duration for which the rate is valid, in months.
-
#effective ⇒ DecNum
readonly
The effective interest rate.
-
#nominal ⇒ DecNum
readonly
The nominal interest rate.
Class Method Summary collapse
-
.to_effective(rate, periods) ⇒ DecNum
convert a nominal interest rate to an effective interest rate.
-
.to_nominal(rate, periods) ⇒ DecNum
convert an effective interest rate to a nominal interest rate.
Instance Method Summary collapse
-
#<=>(rate) ⇒ Numeric
compare two Rates, using the effective rate.
-
#apr ⇒ DecNum
The effective interest rate.
-
#apy ⇒ DecNum
The effective interest rate.
-
#initialize(rate, type, opts = {}) ⇒ Rate
constructor
create a new Rate instance.
- #inspect ⇒ Object
-
#monthly ⇒ DecNum
The monthly effective interest rate.
Constructor Details
#initialize(rate, type, opts = {}) ⇒ Rate
create a new Rate instance
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/finance/rates.rb', line 90 def initialize(rate, type, opts={}) # Default monthly compounding. opts = { :compounds => :monthly }.merge opts # Set optional attributes.. opts.each do |key, value| send("#{key}=", value) end # Set the rate in the proper way, based on the value of type. begin send("#{TYPES.fetch(type)}=", rate.to_d) rescue KeyError raise ArgumentError, "type must be one of #{TYPES.keys.join(', ')}", caller end end |
Instance Attribute Details
#duration ⇒ Integer
Returns the duration for which the rate is valid, in months.
19 20 21 |
# File 'lib/finance/rates.rb', line 19 def duration @duration end |
#effective ⇒ DecNum
Returns the effective interest rate.
22 23 24 |
# File 'lib/finance/rates.rb', line 22 def effective @effective end |
#nominal ⇒ DecNum
Returns the nominal interest rate.
25 26 27 |
# File 'lib/finance/rates.rb', line 25 def nominal @nominal end |
Class Method Details
.to_effective(rate, periods) ⇒ DecNum
convert a nominal interest rate to an effective interest rate
137 138 139 140 141 142 143 144 145 |
# File 'lib/finance/rates.rb', line 137 def Rate.to_effective(rate, periods) rate, periods = rate.to_d, periods.to_d if periods.infinite? rate.exp - 1 else (1 + rate / periods) ** periods - 1 end end |
.to_nominal(rate, periods) ⇒ DecNum
convert an effective interest rate to a nominal interest rate
155 156 157 158 159 160 161 162 163 |
# File 'lib/finance/rates.rb', line 155 def Rate.to_nominal(rate, periods) rate, periods = rate.to_d, periods.to_d if periods.infinite? (rate + 1).log else periods * ((1 + rate) ** (1 / periods) - 1) end end |
Instance Method Details
#<=>(rate) ⇒ Numeric
compare two Rates, using the effective rate
35 36 37 |
# File 'lib/finance/rates.rb', line 35 def <=>(rate) @effective <=> rate.effective end |
#apr ⇒ DecNum
Returns the effective interest rate.
41 42 43 |
# File 'lib/finance/rates.rb', line 41 def apr self.effective end |
#apy ⇒ DecNum
Returns the effective interest rate.
47 48 49 |
# File 'lib/finance/rates.rb', line 47 def apy self.effective end |
#inspect ⇒ Object
107 108 109 |
# File 'lib/finance/rates.rb', line 107 def inspect "Rate.new(#{self.apr.round(6)}, :apr)" end |
#monthly ⇒ DecNum
Returns the monthly effective interest rate.
117 118 119 |
# File 'lib/finance/rates.rb', line 117 def monthly (self.effective / 12).round(15) end |