How to replace a range of elements at specific indexes in array in Ruby

2 Answers

0 votes
arr = [9, 5, 6, 3, 2, 7]
  
arr[1..4] = [10, 20, 30, 40]

print arr


 
 
# run:
  
# [9, 10, 20, 30, 40, 7]

 



answered Aug 31, 2020 by avibootz
0 votes
arr = [9, 5, 6, 3, 2, 7]
  
arr[1..3] = [10, 20, 30, 40, 50, 60]

print arr


 
 
# run:
  
# [9, 10, 20, 30, 40, 50, 60, 2, 7]

 



answered Aug 31, 2020 by avibootz

Related questions

1 answer 107 views
1 answer 214 views
214 views asked Aug 31, 2020 by avibootz
1 answer 284 views
1 answer 268 views
1 answer 222 views
1 answer 210 views
...