Is there a way to refactor this code? The first snippet is what I currently have and the second is the logic I am trying to churn out.
count = 0
until count >= board.length
if board[count] == nil
board[count] = [nil, nil, nil, nil, nil, nil, nil, nil,]
end
count += 1
end
board
On my 4th line, I want to do something like
board[count] = (8.times { board[count] << nil })
I know it is just about as long. I am just curious or want to flush my logic out...thanks!!
Try to use Array
's constructor
Array.new(8)
=> [nil, nil, nil, nil, nil, nil, nil, nil]
Array.new(8, 42)
=> [42, 42, 42, 42, 42, 42, 42, 42]
and pass a block if you need an object and don't want that each instance is the same
Array.new(8) { Hash.new }
=> [{}, {}, {}, {}, {}, {}, {}, {}]