Ruby: assign values to an array starting from a specific index

I have three arrays

g = [1,2,nil,nil,nil,3,nil,nil,nil,nil]
b = [100,200,300,400]
a= [55,45,56]

The result should be:

g= [1,2,55,45,56,3,100,200,300,400]

What I know:
I know where exactly an array a should start (index 2) in g and where array b should start (index 6) in g.

Is there any function that will do something like (or what is the fastest and cleverest way of doing it?)
from a position 2 of g till position 2+a.size assign elements from a
something like g[2:(2+a.size)] = a

(I know I could do it with a loop till (2+a.size), but is there any other method)


Source: ruby

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.