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... ]
我正在为棋盘建模,并想知道是否有一个使用#new填充多维数组。我希望数组中的每个'row'最初都填充'*'即。 [['*','*','*','*','*','*','*','*'],['*','*','*','* * ','*','*','*','*']等...]
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 个解决方案
#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.
如果您对字符串进行破坏性操作,那么您可能需要第一个表单。
#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.
如果您对字符串进行破坏性操作,那么您可能需要第一个表单。