# Array.permutation() Method with Example in Ruby

**Ruby Array.permutation() Method**: Here, we are going to learn about the **Array.permutation() method with examples in Ruby programming language**.

Submitted by Hrithik Chandra Prasad, on January 21, 2020

## Ruby Array.permutation() Method

In this article, we will study about **Array.permutation() method**. You all must be thinking the method must be doing something related to creating permutations of certain elements. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

**Method description:**

This method is a public instance method and defined for the Array class in Ruby's library. This method works in a way that it will take elements from the Array instance and make permutations according to the number passed in the method and then it returns the Array instance itself. This method does not guarantee the order of the elements yielded. This method is invoked with a block or an Array and the result is being converted into the Array instance with the help of **.to_a method**.

If you do not provide any block then the enumerator is returned itself.

**Syntax:**

permutation(n) { |c| block }

**Argument(s) required:**

This method only requires one argument. This argument decides the number of permutations possible from the elements of Array instance.

**Example 1:**

=begin Ruby program to demonstrate permutation method =end a = [1, 2, 3] puts "Array permutation method" print a.permutation(1).to_a puts "" print a.permutation(2).to_a puts "" print a.permutation(3).to_a

**Output**

Array permutation method [[1], [2], [3]] [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]] [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

**Explanation:**

In the above code, you can observe that this method is used to create permutations of the array elements. Permutations are being created based on the argument passed inside the method. N number of combinations can be made and that n can be the multiplication of length of Array instance multiplied by the integer passed in the method.

**Example 2:**

=begin Ruby program to demonstrate permutation method =end a = ["Sucharu","Shalini"] puts "Array permutation method" print a.permutation(1).to_a puts "" print a.permutation(2).to_a puts "" print a.permutation(3).to_a

**Output**

Array permutation method [["Sucharu"], ["Shalini"]] [["Sucharu", "Shalini"], ["Shalini", "Sucharu"]] []

**Explanation:**

In the above example, you can observe that this method works upon String Array instances as well. This method is returning elements after making their permutations. In the case of String Array, the method will return **"nil"** or you can say that permutation is not possible if the length of the Array is lesser than the integer argument passed in the method at the time of invocation.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.