Answer the question
In order to leave comments, you need to log in
How to add an array to itself n number of times with a specific element in ruby?
There is an array = ['one', 'two', 'three']
there is counter
- can be any positive integer
We need to get such a result, with, supposecounter = 3
[ 'counter 1', 'one', 'two', 'three', 'counter 2', 'one', 'two', 'three', 'counter 3', 'one', 'two', 'three']
new_arr = []
counter.each_with_index do |arr, index|
new_arr << arr.unshift("counter #{index}")
end
each_with_index
does not work with a number, maybe there is some way to fix this or another solution that can be applied, tell a beginner.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question