class Array # Enumerates permutation of Array. # Unlike Array#permutation, there are no duplicates in generated permutations. # Instead, elements must be comparable. # # [1,1,2,2,3].unique_permutation(2).to_a # #=> [[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2]] # # Note: [1,1,2,2,3].permutation(2).to_a # #=> [[1, 1], [1, 2], [1, 2], [1, 3], [1, 1], [1, 2], [1, 2], [1, 3], [2, 1], [2, 1], [2, 2], [2, 3], [2, 1], [2, 1], [2, 2], [2, 3], [3, 1], [3, 1], [3, 2], [3, 2]] # # CREDIT: T. Yamada def unique_permutation(n=self.size) return to_enum(:unique_permutation,n) unless block_given? return if n<0||self.size