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

...