def reverse_append(arr, n)
return arr if n < 0
reverse_append(arr, n-1)
arr << n
arr
end
reverse_append([],4) #=> [0, 1, 2, 3, 4]
>
reverse_append([],4)
is called4 >= 0
, the return
statement does not get called.reverse_append([],3)
is called.3 >= 0
, the return
statement does not get called.reverse_append([],2)
is called.2 >= 0
, the return
statement does not get called.reverse_append([],1)
is called.1 >= 0
, the return
statement does not get called.reverse_append([],0)
is called.0 >= 0
, the return
statement does not get called.reverse_append([],-1)
is called.-1 < 0
, the array ([]
) is returned.n = 0
and arr = []
.arr << n
and arr
is returned, so now arr = [0]
.n = 1
and arr = [0]
.arr << n
and arr
is returned, so now arr = [0, 1]
.n = 2
and arr = [0, 1]
.arr << n
and arr
is returned, so now arr = [0, 1, 2]
.n = 3
and arr = [0, 1, 2]
.arr << n
and arr
is returned, so now arr = [0, 1, 2, 3]
.n = 4
and arr = [0, 1, 2, 3]
.arr << n
and arr
is returned, so now arr = [0, 1, 2, 3, 4]
.