Assign to an array and replace emerged nil values
- by Tobias
Greetings!
When assigning a value to an array as in the following, how could I replace the nils by 0?
array = [1,2,3]
array[10] = 2
array # => [1, 2, 3, nil, nil, nil, nil, nil, nil, nil, 2]
If not possible when assigning, how would I do it the best way afterwards? I thought of array.map { |e| e.nil? ? 0 : e }, but well…
Thanks!