# Default argument of Array#permutation

**URL:** <https://www.ruby-forum.com/t/default-argument-of-array-permutation/120604>\
**Category:** Development\
**Created:** [November 19, 2007, 4:51am UTC](https://www.ruby-forum.com/t/default-argument-of-array-permutation/120604 "2007-11-19T04:51:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [November 19, 2007, 4:51am UTC](https://www.ruby-forum.com/t/default-argument-of-array-permutation/120604/1 "2007-11-19T04:51:18Z")

</div>

e$B1sF#$H?=$7$^$9!#e(B

Array#permutation e$B$O%G%U%)%k%H$GA4MWAG$N=gNse(B (e$B$D$^$jCV49e(B)  
e$B$r7W;;$9$k$He(B  
e$B$$$$$H;W$$$^$9!#$I$&$G$7$g$&$+!#e(B

a = [1,2,3]  
a.permutation.to\_a  
#=\> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

# Index: array.c

— array.c (revision 13961)  
+++ array.c (working copy)  
@@ -3001,11 +3001,14 @@

/\*

- call-seq:

- 
  - 

```
ary.permutation { |p| block } -> array

```

- 
  - 

```
ary.permutation -> enumerator

```

  - 

```
ary.permutation(n) { |p| block } -> array

```

  - 

```
ary.permutation(n) -> enumerator

```

  - 
  - When invoked with a block, yield all permutations of length _n_
  - of the elements of _ary_, then return the array itself.

- 
  - If _n_ is not specified, yield all permutations of all  
elements.
  - The implementation makes no guarantees about the order in which
  - the permutations are yielded.
  - 

@@ -3013,6 +3016,7 @@  
\*

- Examples:
- 

```
a = [1, 2, 3]

```

- 
  - 

```
a.permutation.to_a #=>

```

[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

- 

```
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]]  
@@ -3021,13 +3025,15 @@  
\*/

static VALUE  
-rb\_ary\_permutation(VALUE ary, VALUE num)  
+rb\_ary\_permutation(int argc, VALUE \*argv, VALUE ary)  
{

- VALUE num;  
long r, n, i;

- RETURN\_ENUMERATOR(ary, 1, &num); /\* Return enumerator if no block  
\*/
- r = NUM2LONG(num); /\* Permutation size from argument  
\*/
- n = RARRAY\_LEN(ary); /\* Array length \*/

- RETURN\_ENUMERATOR(ary, argc, argv); /\* Return enumerator if no  
block \*/

- n = RARRAY\_LEN(ary); /\* Array length \*/

- rb\_scan\_args(argc, argv, “01”, &num);

- r = NIL\_P(num) ? n : NUM2LONG(num); /\* Permutation size from  
argument \*/

- rb\_define\_method(rb\_cArray, “permutation”, rb\_ary\_permutation, 1);

- rb\_define\_method(rb\_cArray, “permutation”, rb\_ary\_permutation, -1);  
rb\_define\_method(rb\_cArray, “combination”, rb\_ary\_combination, 1);  
rb\_define\_method(rb\_cArray, “product”, rb\_ary\_product, -1);

---

<div class="post-metadata">

**Author:** ![Yusuke\_ENDOH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/c89c15/32.png) [@Yusuke\_ENDOH](https://www.ruby-forum.com/u/Yusuke_ENDOH)\
**Post date:** [November 19, 2007, 8:00am UTC](https://www.ruby-forum.com/t/default-argument-of-array-permutation/120604/2 "2007-11-19T08:00:09Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32309] default argument of Array#permutation”  
on Mon, 19 Nov 2007 12:50:30 +0900, “Yusuke ENDOH” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:  
|  
|e$B1sF#$H?=$7$^$9!#e(B  
|  
|Array#permutation e$B$O%G%U%)%k%H$GA4MWAG$N=gNse(B (e$B$D$^$jCV49e(B) e$B$r7W;;$9$k$He(B  
|e$B$$$$$H;W$$$^$9!#$I$&$G$7$g$&$+!#e(B  
|  
| a = [1,2,3]  
| a.permutation.to\_a  
| #=\> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

e$B$U$`!"$$$$$s$8$c$J$$$G$7$g$&$+!#e(B
