Andrew Grimm
Andrew Grimm

Reputation: 81548

Expand array with preferred default

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.

Upvotes: 8

Views: 1474

Answers (3)

dbenhur
dbenhur

Reputation: 20408

Array#fill might be your ticket.

arr = []

index = 2
arr.fill( "", arr.length...index )
arr[index] = "!"
# => ["", "", "!"]

index = 5
arr.fill( "", arr.length...index )
arr[index] = "!"
# => ["", "", "!", "", "", "!"]

index = 1
arr.fill( "", arr.length...index )
arr[index] = "!"
#=> ["", "!", "!", "", "", "!"]

Upvotes: 3

I had a rapid look to the Array documentation and I didn't find nothing useful for this...

...but if I understood well you need to replace nil values with empty strings ("") before to export your data to the spreadsheet. What about call to_s on each element of the array before do that?:

arr.map! &:to_s

Upvotes: 1

megas
megas

Reputation: 21791

What about using hash as array? It might look like this:

h = Hash.new do |hash,key|
  0.upto(key) { |i| hash[i] = "" unless hash.has_key?(i) }
end

h[5]
h[0] #=> ""
h[4] #=> ""
h.keys #=> [0, 1, 2, 3, 4, 5]

Maybe this approach require some additional tweaks to satisfy your demands, for example you can define method size and so on.

P.S. Get an array

h.values #=> ["", "", "", "", "", ""]

Upvotes: 3

Related Questions