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 109 views
1 answer 221 views
221 views asked Aug 31, 2020 by avibootz
1 answer 296 views
1 answer 278 views
1 answer 233 views
1 answer 216 views
...