Я бы, наверное, просто решил это с помощью Enumerable#enum_for(:each_with_index)
require 'enumerator'
values = [3,0,6,11,77,2,1,5,48,9,122,0,43,13,564]
subset_with_indexes = values.enum_for(:each_with_index).select { |v,i| i % 5 == 0 }
#=> [ [3,0], [2,5], [122,10] ]
subset_sum = subset_with_indexes.inject(0) { |s,(v,i)| s+v }
#=> 127
subset_with_indexes.each do |v,i|
values[i] = subset_sum - v
end
values #=> [124, 0, 6, 11, 77, 125, 1, 5, 48, 9, 5, 0, 43, 13, 564]
Или
require 'enumerator'
values = [3,0,6,11,77,2,1,5,48,9,122,0,43,13,564]
values_with_indexes = values.enum_for(:each_with_index)
subset_sum = values_with_indexes.inject do |s,(v,i)|
i % 5 == 0 ? s + v : s
end #=> 127
new_values = values_with_indexes.map do |v,i|
i % 5 == 0 ? subset_sum - v : v
end #=> [124, 0, 6, 11, 77, 125, 1, 5, 48, 9, 5, 0, 43, 13, 564]