Q:

Generate different combinations of arrays using positive integers as elements

Given a certain integer n and a different integer k (both positive) I want to generate all the possible different arrays of size k containing integers in the interval [0..n]. For example with n = 2 and k = 2 I want to generate an array of arrays that contains [0,0], [0,1], [1,0], [0,2], [1,1], [2,0], [1,2], [2,1], [2,2]. So the result should be
result = [[0,0], [0,1], [1,0], [0,2], [1,1], [2,0], [1,2], [2,1], [2,2]]

The order of the elements doesn't matter.

A:

Use Array#repeated_permutation:
(0..2).to_a.repeated_permutation(2).to_a
# => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]

