I want to add an array to a two dimensional array like this:
arrays = [[8300, 6732, 4101, 3137, 3097], [1088, 647, 410, 138, 52], [623, 362, 191, 25, 0]]
new_array = [10, 100, 1000]
arrays.map.with_index{|v,i| v << new_array[i]}
# => [[8300, 6732, 4101, 3137, 3097, 10], [1088, 647, 410, 138, 52, 100], [623, 362, 191, 25, 0, 1000]]
It works well, but I want to know if there is more simpler way to accomplish this behavior.
I appreciate any suggestion.
flattenandflatten(1)have a limatation:[[1],[2]].zip([3,[4,5]]).map { |x| x.flatten(1) } #=> [[1, 3], [2, 4, 5]]. I don't know if that's a problem in your app. If you did not want to alterarrays, you could write[1,2].zip([3,[4,5]]).map { |a,b| [a]+[b] } #=> [[1, 3], [2, [4, 5]]].