伪·递归累加(几千就爆栈了貌似……)
class Maths
def initialize
@total=0
end
attr_accessor:reader
def addtotal(a,b)
@total+=a
return(@total) if a==b
#return if a==b
self.addtotal(a+1,b)
end
p Maths.new.addtotal(1,1000) #=>500500
end
class Maths
def initialize
@total=0
end
attr_accessor:reader
def addtotal(a,b)
@total+=a
return(@total) if a==b
#return if a==b
self.addtotal(a+1,b)
end
p Maths.new.addtotal(1,1000) #=>500500
end