I have hundreds of arrays that am normalizing for a CSV.
[
["foo", "tom", nil, 1, 4, "cheese"],
["foo", "tom", "fluffy",nil, 4],
["foo", "tom", "fluffy",1, nil],
...
]
Currently to make them all equal length i am finding the max length and setting to a value.
rows.each { |row| row[max_index] ||= nil }
this is cool because it makes the array length equal to the new length.
Instead of appending a bunch of nils at the end I needed to append COLUMN_N where N is the index (1-based).
table_rows.each do |row|
last_index = row.length - 1
(last_index..max_index).to_a.each { |index| row[index] ||= "COLUMN_#{index+1}" }
end
Which seemed like an awkward way to have a default value that is a function of the index.