如何使用多维数组收集

时间:2021-12-12 21:27:18
d = [2,4,6]
d.collect{ |i| i * 2 } #=> [4,8,12]

I tried to do the same with multi-dimensional array

我试图用多维数组做同样的事情

d = [[1,3],[2,4]]
d.collect { |i,j| i*2, j*2 } #=> getting syntax error

1 个解决方案

#1


2  

To represent array, you need to surround them with [ and ]:

要表示数组,您需要用[和]包围它们:

d.collect { |i,j| [i*2, j*2] }
#                 ^        ^
# => [[2, 6], [4, 8]]

#1


2  

To represent array, you need to surround them with [ and ]:

要表示数组,您需要用[和]包围它们:

d.collect { |i,j| [i*2, j*2] }
#                 ^        ^
# => [[2, 6], [4, 8]]