0015 Cycle

You'll go round in circles!

This method of Enumerable allows it to be repeated a defined number of times.

0015-cycle_1.png

It can be used :

  • with a block to iterate directly
  • without a block, to obtain an Enumerator.
  • without parameters to repeat indefinitely

0015-cycle_2.png

The infinite Enumerator is perfect for alternating colors, for example.

0015-cycle_3.png

With Ruby’s flexibility, we wanted to test a partial cycle, but the value is truncated!

0015-cycle_4.png

Link to Ruby documentation for cycle.