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

instance method Array#permutation

permutation(n) { |p| block } -> Array
permutation(n) -> Enumerable::Enumerator

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

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

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

例:

a = [1, 2, 3]
a.permutation(1).to_a  #=> [[1],[2],[3]]
a.permutation(2).to_a  #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
a.permutation(3).to_a  #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutation(0).to_a  #=> [[]]: one permutation of length 0
a.permutation(4).to_a  #=> []  : no permutations of length 4

[SEE_ALSO] Array#combination

class Array