码:
nums = [1,2,3] nums.each { |i| nums << i + 1 }
我的解决方案
nums = [1,3] adjustments = [] nums.each { |i| adjustments << i + 1 } nums += adjustments
您可以在应用每个数组之前复制数组.
nums = [1,3] nums.dup.each { |i| nums << i + 1 }
另一种方法是追加map给出的额外元素:
nums = [1,3] nums += nums.map { |i| i + 1 }