I have an array of in Ruby. I want to:
Get a subset of the elements based on their position in the array - say every 5th element. I can do this with each_index, or extend and create a select_with_index method.
Perform some operation on the subset that depends on the entire subset - let's say subset.map{|element| subset.sum - element}
This is the bit I'm stuck on: Create a new array with the correct items replaced by the items in step 2. Eg:
So my highly convoluted example might have:
Start: [3,0,6,11,77,2,1,5,48,9,122,0,43,13,564]
Select: [3,2,122]
Map: [124,125,5]
Replace: [124,0,6,11,77,125,1,5,48,9,5,0,43,13,564]
How can I perform the replacement in an elegant fashion? Is there a way to create method that would take combine the two arrays and take a block {|i| i % 5 == 0}?
(This is motivated by an approach to writing a compact Sudoku solver in order to learn some more Ruby...)
EDIT: Have changed the example values. Hopefully this is clearer now.