Agreed. A better solution here is to have a generic accessor on the Array that can return a default value for out-of-bounds accesses. There almost is one already, the two-argument version of fetch(), except that interprets negative indices as counting from the end. But if we create a variant that doesn't treat negative indices specially then the code becomes very simple
result = input.map.with_index do |_, i|
input.better_fetch(i-1, 0) + input.better_fetch(i+1, 0)
end.to_a