0

I'm modeling a chessboard and was wondering if there is a populate a multidimensional array using #new. I want each 'row' in the array to be initially populated with a '*' ie. [[ '*', '*', '*', '*', '*', '*', '*', '*'], [ '*', '*', '*', '*', '*', '*', '*', '*'], etc... ]

empty_frame = Array.new(8){[]}   #Can you use code block to fill here?

Or do i need to iterate through each 'row' to populate it?

1 Answer 1

3

You did the first level correctly using a block. Why not do the same with the second level?

empty_frame = Array.new(8){Array.new(8){"*"}}

or

empty_frame = Array.new(8){Array.new(8, "*")}

If you are doing destructive operations on the string, then you probably need the first form.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.