How can I auto-increment a variable so each time it is used, it is incremented by one, starting with 0?
For example:
i = i+1 || 0
arr[i] = "foo"
arr[i] = "bar"
arr[i] = "foobar"
arr #=> ["foo","bar","foobar"]
I'm getting a NoMethodError undefined method '+' for nil:NilClass
ias an array index for assignment as in your example? If it is, why not just use the<<array append operator to fill the array?i = i+1 || 0. It's not idiomatic, nor is it particularly readable or maintainable. Instead do the normal thing and assigni = 0, then usei += 1wherever you need it to increment.