Method: Finance::Rate#initialize

Defined in:
lib/finance/rates.rb

#initialize(rate, type, opts = {}) ⇒ Rate

create a new Rate instance

Examples:

create a 3.5% APR rate

Rate.new(0.035, :apr) #=> Rate(0.035, :apr)

Parameters:

  • rate (Numeric)

    the decimal value of the interest rate

  • type (Symbol)

    a valid rate type

  • opts (optional, Hash) (defaults to: {})

    set optional attributes

Options Hash (opts):

  • :duration (String)

    a time interval for which the rate is valid

  • :compounds (String) — default: :monthly

    the number of compounding periods per year

See Also:



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