 # Local variable bug

I am newbie in Ruby, I cannot find the bug on my financial program.
The question is related to local variable in block.
Here is a quick example:

a = 5000
r = 0.06
p = 100
(1…5).each {|i|
puts "#{i}\t#{a}\t#{ar}\t#{p}" # <-- the #{a} is not print
correctly, why?
a -= (p - a
r)
}

Thanks for help.

···

Below is my financial program, skip it if you are not interesting.
In fact, I want to know how the bank calcule the payment per year for
a loan.

Below is my quick and throw away program. You can play it.
BTW: If anyone know the web site to do this calculation. (Maybe some
formula out there)
My result still a little bit different then the bank.
(because tax? or other fee charge by the bank … )

=begin

Calculate the payment for loan.
This program try to find out the payment per year.
It uses array of 2 elements to simulate symbolic algebra
calculation.
[x,y] => x is real value, y is coefficient of payment per year.
Note: no robust error checking for input value. GUI todo.

=end

#Re-define Array for our use. Note, it may-be better create our own
class.
class Array
def (int)
self.map{|x| x
int}
end

def +(array)
x = self.dup
self.each_index{|i| x[i] += array[i]}
x
end

def -(array)
self + (array * -1)
end
end

print "Capital = ? "
capital_in = gets.to_i
capital = [capital_in, 0] # to our format

print "Years = ? "
years = gets.to_i

print "interest rate = ? "
rate = gets.to_f

# calculate the sum of each year’s interest

sum_int = [0,0]
(1…years).each {
interest = capital * rate
sum_int += interest
capital -= ([0,1] - interest) # [0,1] means the payment for a year
}

# ==> X = (capital_in + sum_int) / (years - sum_int)

payment = (capital_in + sum_int) / (years - sum_int)

# now, output in good format.

puts “-” * 30
puts "you loan from bank:"
puts "capital = #{capital_in}"
puts "years = #{years}"
puts "interest rate = #{rate}"
puts
puts "payment/year = #{payment}, payment/month = #{payment/12}"
puts “total payment = #{paymentyears}"
puts "total bank gain = #{payment
years-capital_in}”

#=begin More detail information for each year
capital = capital_in
puts “\nYear\tCapital\tInterest\tPayment”
(1…years).each {|year|
# puts "#{year}\t#{capital}\t#{capitalrate}\t#{payment}" # <–
the #{capital} is not print correctly, why ??
printf “%i\t%.2f\t%.2f\t%.2f\n”, year, capital, (capital
rate),
payment
capital -= (payment - capital*rate)
}
#=end

Email55555 wrote:

I am newbie in Ruby, I cannot find the bug on my financial program.
The question is related to local variable in block.
Here is a quick example:

a = 5000
r = 0.06
p = 100
(1…5).each {|i|
puts “#{i}\t#{a}\t#{a*r}\t#{p}” # <-- the #{a} is not print
^

You escape the # sign here, so the #{a} part isn’t parsed as an
expression to be interpolated into the string.
Remove the \ and it should work somewhat better.

···

([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

Delete ‘’ before ‘#’
#{i}\t#{a}… => #{i}\t#{a}…

Guaracy

···

----- Original Message -----
From: “Email55555” tran55555@yahoo.com

I am newbie in Ruby, I cannot find the bug on my financial program.
The question is related to local variable in block.
Here is a quick example:

a = 5000
r = 0.06
p = 100
(1…5).each {|i|
puts “#{i}\t#{a}\t#{a*r}\t#{p}” # <-- the #{a} is not print
correctly, why?

===================
HP: www.guaracy.cjb.net

a = 5000
r = 0.06
p = 100
(1…5).each {|i|
puts “#{i}\t#{a}\t#{a*r}\t#{p}” # <-- the #{a} is not print

Remove the “” before the “#” in position 6 (0 based) in the string.

···

## =====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell

Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

Oh, that’s right, the simple “” is too much.
Thanks all for help.