我有一个不确定长度的数组,让我们说[1,2,3,4,5].我想从3之前的所有内容中减去1,并在3之后的所有内容中加1,制作示例[0,1,3,5,6].如果没有3,则为所有内容添加1:[1,2,4,5] = [2,3,5,6].这样做最优
a = [1, 2, 3, 4, 5] n = a.index(3) || -1 a.map.with_index{|e, i| e + (i <=> n)} # => [0, 1, 3, 5, 6]
a = [1, 2, 3, 4, 5] n = a.index(3) || -1 a.map.with_index{|e, i| e + (i <=> n)} # => [0, 1, 3, 5, 6]