I am attempting to take two values times and number and return an array of number repeated times times. Here is an example:
replicate(3, 5) # => [5, 5, 5]
Here is my attempt 1:
@array = []
def replicate(times, number)
return [] if times <= 0
@array << number
replicate(times - 1, number)
@array
end
When I run each test case once in isolation, I pass everything. However, when I run them all at once, it fails; @array contains all the values for every test case and @array will look like this at the end of the entire test suite:
@array # => [5, 5, 5, 1, 1, 1, 1, 2, 2]
Here is implementation two:
def replicate(times, number)
return [] if times <= 0
array = []
array << number
replicate(times - 1, number)
array
end
This will return only one value because recursion will create a local copy for every run.
How can I return an array that will make the test cases pass? I can't use global or instance variables, or a local copy of an array. Is there something I can use in between?