class Fib_cal def initialize() puts "This is initialize!" end def fib(n) i,j,a=1,1,1 while a<n if(i<=j) i+=j else j+=i end a+=1 end if(i<=j) puts "#{i}" else puts "#{j}" end puts "fib end!" end def fib_simple(n) res = fib_simple(n-1)+fib_simple(n-2) puts "#{res}" puts "fib_simple end!" end def fib_list(n) i,j,a=1,1,1 res=[1,1] while a<=n if(i<=j) i+=j res[a+1] = i else j+=i res[a+1] = j end a+=1 end res.each{|i| puts i} puts "fib_list end!" end end
fib_test = Fib_cal.new fib_test.fib(5) fib_test.fib_list(5) fib_test.fib_simple(5)
运行报错了:
Fib_cal.rb:23:in fib_simple': stack level too deep (SystemStackError)
from Fib_cal.rb:23:in
fib_simple'
from Fib_cal.rb:48
This is initialize!
5
fib end!
1
1
2
3
5
8
13
fib_list end!