Skip to content
instance method

Array#repeated_permutation

(::int n) { (::Array[Elem] p) -> void } -> self
(::int n) -> ::Enumerator[::Array[Elem], self]
repeated_permutation(size) {|permutation| ... } → self
repeated_permutation(size) → new_enumerator

With a block given, calls the block with each repeated permutation of length size of the elements of self; each permutation is an array; returns self. The order of the permutations is indeterminate.

If a positive integer argument size is given, calls the block with each size-tuple repeated permutation of the elements of self. The number of permutations is self.size**size.

Examples:

  • size is 1:

    Example
    p = []
    [0, 1, 2].repeated_permutation(1) {|permutation| p.push(permutation) }
    p # => [[0], [1], [2]]
    
  • size is 2:

    Example
    p = []
    [0, 1, 2].repeated_permutation(2) {|permutation| p.push(permutation) }
    p # => [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
    

If size is zero, calls the block once with an empty array.

If size is negative, does not call the block:

Example
[0, 1, 2].repeated_permutation(-1) {|permutation| fail 'Cannot happen' }

With no block given, returns a new Enumerator.

Related: see Methods for Combining.

See also #repeated_combination #replace #reject! #reverse also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0