Jeremy Richards Jeremy Richards - 4 months ago 12
Ruby Question

Dynamically create 2d array ruby

Is it possible to create the following 2d array dynamically:

[[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2], [4, 1], [4, 2]]


Eg.

(1..4).to_a
#=> [1, 2, 3, 4]
(1..2).to_a
#=> [1, 2]


Combine this somehow?

nus nus
Answer

Array#product is the method you're looking for:

(1..4).to_a.product (1..2).to_a