Q:

How to build an amortization table in ruby?

I'm trying to build an amortization table in Ruby to:

Calulcate monthly payment.
monthly interest costs.
Showing how much of the payment actually reduces the loan balance 
(or the amount you owe).
The table shows how and when a loan will be paid off.
Figure out how much owe after any number of payments.
Compare loan offers to see how much interest pay with each loan etc.

To solve this I thought to create two empty arrays, one array will hold all monthly values and the second will hold all the monthly arrays.
loan_amount = 250000
rate = 0.035
periods = 60
loan_time = 5 years

periods.times do |period|

 # the monthly payment
 monthly_payment = loan_amount *( (rate * ( 1 + rate)**periods)  / ( ( 1 + rate )**periods - 1) )

end

How can I decrement a value in ruby?
e.g. I need to decrement the loan_amount with the monthly payment

A:

If you are going to calculate the payment every time through the loop, you'll also want to decrement the periods and keep the amount owed moving. Something like this:
periods.times do |period|

   # the monthly payment
    monthly_payment = loan_amount * ( (rate * ( 1 + rate)**periods)  / ( ( 1 + rate )**periods - 1) )

    periods -= 1
    loan_amount = loan_amount * (1 + rate) - monthly_payment
end

You should notice though that the monthly payment remains constant. You could really calculate it before the loop and then it would look like this:
# the monthly payment
monthly_payment = loan_amount * ( (rate * ( 1 + rate)**periods)  / ( ( 1 + rate )**periods - 1) )

periods.times do |period|
    loan_amount = loan_amount * (1 + rate) - monthly_payment
end

Then loan_amount will be the amount owed at the end of each month.
If the payment seems really high, that's because your rate is probably the yearly interest rate and it can be divided by 12.

