14km

Apr 13

Be mindful of array copying

Trying

irb> d = [1,2,3,4]
=> [1,2,3,4]
irb> d[0..-2].each_with_index {|k,i| d[i+1] = "x"; puts k }
1
2
3
=> [1,2,3]
irb> d
[1, "x", "x", "x"]

Reasonable, d[0..-2] would make a copy of d

irb> d.each_with_index {|k,i| d[i+1] = "x" if i < 3; puts k  }
1
x
x
x
=>  [1,2,3]
irb> d
[1,  "x", "x", "x"]