# Enumerable.cycle(n)

**URL:** <https://www.ruby-forum.com/t/enumerable-cycle-n/134516>\
**Category:** Development\
**Created:** [April 11, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/enumerable-cycle-n/134516 "2008-04-11T16:56:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [April 11, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/enumerable-cycle-n/134516/1 "2008-04-11T16:56:21Z")

</div>

ã€€Enumerable#cycle ã‚’å›žæ•°æŒ‡å®šå¯èƒ½ã«ã™ã‚‹ã®ã¯ã©ã†ã§ã—ã‚‡ã†ã‹ã€‚

(1…3).cycle(2) { |obj| … } # 1,2,3,1,2,3

ã€€ä½•å›žç›®ãªã®ã‹ã‚’æ¸¡ã™ #cycle\_with\_counter { |obj, counter| … } ã‚‚  
ã»ã—ã„ã‹ã‚‚ã€‚(ä¸‹ã®ãƒ‘ãƒƒãƒã«ã¯å«ã¾ã‚Œã¦ã„ã¾ã›ã‚“)

# Index: array.c

— array.c (revision 15977)  
+++ array.c (working copy)  
@@ -2938,25 +2938,41 @@  
/\*

- call-seq:
- 

```
ary.cycle {|obj| block }

```

- 
  - 

```
ary.cycle(n) {|obj| block }

```

  - 

- 
  - Calls _block_ repeatedly forever.

- 
  - Calls _block_ for each element repeatedly _n_ times or

- 
  - forever if none or nil is given. If a non-positive number is

- 
  - given or the array is empty, does nothing. Returns nil if the

- 
  - loop has finished without getting interrupted.
  - 
  - 

```
a = ["a", "b", "c"]

```

  - 

```
a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever.

```

- 
  - 

```
a.cycle(2) {|x| puts x } # print, a, b, c, a, b, c.

```

  - 

\*/

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

- long i;

- long n, i;
- VALUE nv = Qnil;

- RETURN\_ENUMERATOR(ary, 0, 0);
- while (RARRAY\_LEN(ary) \> 0) {
- for (i=0; i\<RARRAY\_LEN(ary); i++) {
- 

```
 rb_yield(RARRAY_PTR(ary)[i]);

```

- }

- rb\_scan\_args(argc, argv, “01”, &nv);
- 
- RETURN\_ENUMERATOR(ary, argc, argv);
- if (NIL\_P(nv)) {
- 

```
   n = -1;

```

}
- else {
- 

```
   n = NUM2LONG(nv);

```

- 

```
   if (n <= 0) return Qnil;

```

- }
- 
- while (RARRAY\_LEN(ary) \> 0 && (n \< 0 || 0 \< n–)) {
- 

```
   for (i=0; i<RARRAY_LEN(ary); i++) {

```

- 

```
       rb_yield(RARRAY_PTR(ary)[i]);

```

- 

```
   }

```

- }  
return Qnil;  
}

@@ -3430,7 +3446,7 @@  
rb\_define\_method(rb\_cArray, “shuffle!”, rb\_ary\_shuffle\_bang, 0);  
rb\_define\_method(rb\_cArray, “shuffle”, rb\_ary\_shuffle, 0);  
rb\_define\_method(rb\_cArray, “choice”, rb\_ary\_choice, 0);

- rb\_define\_method(rb\_cArray, “cycle”, rb\_ary\_cycle, 0);

- rb\_define\_method(rb\_cArray, “cycle”, rb\_ary\_cycle, -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);  
Index: enum.c  
===================================================================  
— enum.c (revision 15977)  
+++ enum.c (working copy)  
@@ -1656,33 +1656,48 @@  
/\*

- call-seq:
- 

```
enum.cycle {|obj| block }

```

- 
  - 

```
enum.cycle(n) {|obj| block }

```

  - 

- 
  - Calls _block_ for each element of _enum_ repeatedly

- 
  - forever. Returns nil if and only if the collection is empty.

- 
  - Calls _block_ for each element of _enum_ repeatedly _n_

- 
  - times or forever if none or nil is given. If a non-positive

- 
  - number is given or the collection is empty, does nothing. Returns

- 
  - nil if the loop has finished without getting interrupted.

- 
  - 
  - Enumerable#cycle saves elements in an internal array so changes
  - to _enum_ after the first pass have no effect.
  - 
  - 

```
a = ["a", "b", "c"]

```

  - 

```
a.cycle {|x| puts x } # print, a, b, c, a, b, c,.. forever.

```

- 
  - 

```
a.cycle(2) {|x| puts x } # print, a, b, c, a, b, c.

```

  - 

\*/

static VALUE  
-enum\_cycle(VALUE obj)  
+enum\_cycle(int argc, VALUE \*argv, VALUE obj)  
{  
VALUE ary;

- long i, len;

- VALUE nv = Qnil;
- long n, i, len;

- RETURN\_ENUMERATOR(obj, 0, 0);

- rb\_scan\_args(argc, argv, “01”, &nv);
- 
- RETURN\_ENUMERATOR(obj, argc, argv);
- if (NIL\_P(nv)) {
- 

```
   n = -1;

```

- }
- else {
- 

```
   n = NUM2LONG(nv);

```

- 

```
   if (n <= 0) return Qnil;

```

- }  
ary = rb\_ary\_new();  
RBASIC(ary)-\>klass = 0;  
rb\_block\_call(obj, id\_each, 0, 0, cycle\_i, ary);  
len = RARRAY\_LEN(ary);  
if (len == 0) return Qnil;

- for (;😉 {
- for (i=0; i\<len; i++) {
- 

```
 rb_yield(RARRAY_PTR(ary)[i]);

```

- }

- while (n \< 0 || 0 \< --n) {
- 

```
   for (i=0; i<len; i++) {

```

- 

```
       rb_yield(RARRAY_PTR(ary)[i]);

```

- 

```
   }

```

}  
return Qnil; /\* not reached \*/  
}  
@@ -1741,7 +1756,7 @@  
rb\_define\_method(rb\_mEnumerable, “take\_while”, enum\_take\_while, 0);  
rb\_define\_method(rb\_mEnumerable, “drop”, enum\_drop, 1);  
rb\_define\_method(rb\_mEnumerable, “drop\_while”, enum\_drop\_while, 0);

- rb\_define\_method(rb\_mEnumerable, “cycle”, enum\_cycle, 0);

- rb\_define\_method(rb\_mEnumerable, “cycle”, enum\_cycle, -1);

---

<div class="post-metadata">

**Author:** ![Akinori\_MUSHA](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b782af/32.png) [@Akinori\_MUSHA](https://www.ruby-forum.com/u/Akinori_MUSHA)\
**Post date:** [April 12, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/enumerable-cycle-n/134516/2 "2008-04-12T14:28:52Z")

</div>

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

In message “Re: [ruby-dev:34346] Enumerable.cycle(n)”  
on Fri, 11 Apr 2008 23:55:44 +0900, “Akinori MUSHA”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|e$B!!e(BEnumerable#cycle e$B$r2s?t;XDj2DG=$K$9$k$N$O$I$&$G$7$g$&$+!#e(B  
|  
|(1…3).cycle(2) { |obj| … } # 1,2,3,1,2,3

e$B%?%$%\_%s%0E\*$K$"$^$jNI$/$J$$$N$G$9$,!"JXMx$=$&$J$N$GF3F~$7$Fe(B  
e$B$b$h$$$N$G$O$J$$$+$H;W$$$^$9!#e(B

|e$B!!2?2sL$J$N$+$rEO$9e(B #cycle\_with\_counter { |obj, counter| … } e$B$be(B  
|e$B$[$7$$$+$b!#e(B(e$B2\<$N%Q%C%A$K$O4^$^$l$F$$$^$;$se(B)

e$B$&!\<$s!"$3$A$i$OJ]N1!#e(B
