Ruby 3.2.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Arrayクラス > repeated_permutation
repeated_permutation(n) { |p| ... } -> self
[permalink][rdoc]repeated_permutation(n) -> Enumerator
サイズ n の重複順列をすべて生成し,それを引数としてブロックを実行します。
得られる順列の順序は保証されません。ブロックなしで呼び出されると, 順列を生成する Enumerator オブジェクトを返します。
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
ブロックが与えられた場合、作成した配列の各要素を引数としてブロックを実行して self を返します。
a = [1, 2]
result = []
a.repeated_permutation(3) {|e| result << e} # => [1,2]
result #=> [[1,1,1],[1,1,2],[1,2,1],[1,2,2],
# [2,1,1],[2,1,2],[2,2,1],[2,2,2]]
[SEE_ALSO] Array#repeated_combination, Array#permutation