RUBY Enumberable Methods

04-10-15

In the Ruby language, there are many ways to use methods to help solve problems or make the code do something for you. Ruby like most other languages have built in ways to work through data to get certain information back so that one can use it for other purposes. One of those methods are #cycle.

Now let's discuss a little about enumeration and what that means. Wikipedia defines enumeration as a complete, ordered listing of all the items in a collection. The act of enumerating is to mention seperately as if in counting; name one by one; specify, as in a list. Ruby has a number of ways to do this.

With the method #cycle, when called on a set of numbers, can perform a block of code on each and every item in that set of numbers. You can also call it to enumerate over the set as many times as you like by passing it a value for however many times you would like it to go over teh set of numbers. Take note that if you do not pass it a number, it will repeat itself indefintely and cause a loop that never ends.

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.