how do i add these two element-wise so the result is another 3-element array? [*1..3] + [*4..6] in ruby

In Ruby, you can add two arrays element-wise using the zip method. Here's the code to add the arrays [*1..3] and [*4..6] element-wise to get another 3-element array:

main.rb
array1 = [*1..3]
array2 = [*4..6]

result = array1.zip(array2).map { |a, b| a + b }

puts result.inspect
105 chars
7 lines

This will output [5, 7, 9], which is the result of adding the two arrays element-wise.

related categories

gistlibby LogSnag