Ruby 1.9.3 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Arrayクラス > repeated_permutation

instance method Array#repeated_permutation

repeated_permutation(n) { |p| ... } -> Array
repeated_permutation(n) -> Enumerator

サイズ n の重複順列をすべて生成し,それを引数としてブロックを実行します。

得られる順列の順序は保証されません。ブロックなしで呼び出されると, 順列 を生成する Enumerator オブジェクトを返します。

[PARAM] n:
生成する配列のサイズ。

例:

a = [1, 2]
a.repeated_permutation(1).to_a  #=> [[1], [2]]
a.repeated_permutation(2).to_a  #=> [[1,1],[1,2],[2,1],[2,2]]
a.repeated_permutation(3).to_a  #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2],
                                #    [2,1,1],[2,1,2],[2,2,1],[2,2,2]]
a.repeated_permutation(0).to_a  #=> [[]] # one permutation of length 0

[SEE_ALSO] Array#repeated_combination, Array#permutation

class Array