The documentation for Array#[]= notes that

If indices are greater than the current capacity of the array, the array grows automatically.

When it does grow automatically, it does so with nil values:

arr = []
arr[2] = "!"
arr # => [nil, nil, "!"]

Is it possible to specify what the default is for those first two values?

Currently, I'm doing

arr = []
index = 2
currently_uninitialized_value_range = (arr.length)...(index)
default_values = currently_uninitialized_value_range.map{ "" }
arr[currently_uninitialized_value_range] = default_values
arr[index] = "!"
arr # => ["", "", "!"]

Which is a little verbose.

I'm using an array, rather than a hash, because they're representing the values I'm going to be inputting into a spreadsheet, and the library I'm using (Axlsx) prefers to have data added row by row.

Edit
Report