Bala
Bala

Reputation: 11244

How to use collect with multi-dimensional array

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

Upvotes: 0

Views: 60

Answers (1)

falsetru
falsetru

Reputation: 369074

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

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

Upvotes: 2

Related Questions