In Depth look at the Cycle Enumerable Method

Building a Method Foundation

August 2, 2015

The cycle method is relatively simple to understand but when used in conjunction with several other methods, it becomes very powerful. The method can be called on both arrays and hashes and will simply loop through the items indefinitely and execute the pre block associated. For example:

 days = %w{Monday Tuesday Wednesday Thursday Friday Saturday Sunday}
        days.cycle{|x| p x}
        => Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        Sunday
        Monday
        Tuesday
        ...


This loop will continue forever (or until ctrl C is hit) unless we implement a break statement within the pre block:

 days = %w{Monday Tuesday Wednesday Thursday Friday Saturday Sunday}
        days.cycle{|x|
          p x
          break if x = "Saturday"
        }
        => Monday
        Tuesday
        Wednesday
        Thursday
        Friday
        Saturday
        


The same would apply for a hash, but the output would be the key/value pair in the form of an array (key as element 0, value as element 1).

The beauty of cycle comes in to play when the method is used to create a new object like so:

        days = %w{Monday Tuesday Wednesday Thursday Friday Saturday Sunday}
        days_of_week = days.cycle
        


Now we have a cycle object that we can apply a few different methods to. The first two we will look at are `next` and `rewind`. These methods help us step through our cycle object. If next is called on the object, it will return the next item in the cycle.

        days_of_week.next
        => Monday
        days_of_week.next
        => Tuesday
        


As we continue to call next, the output will continue to change as we cycle through the items in the cycle object. If we get to the last item and call next, it will start again at the beginning of the cycle. If we find ourselves in the middle of a cycle and want to return to the beginning, `rewind` can be called on the cycle object:

        days_of_week.next
        => Monday
        days_of_week.next
        => Tuesday
        days_of_week.rewind
        days_of_week.next
        => Monday
        

The final method we will look at that combines with the cycle method is zip. Zip can be used to combine two arrays to create several new arrays that contain the elements from each array that are in the same position. In our case, we will use it to combine our cycle object with an array. The cycle object will continue to spit out values until it has matched up with all of the items in our array:

        moons = ["New", "Waning Crescent", "Third Quarter", "Waning Gibbous", "Full", "Waxing Gibbous", "First Quarter", "Waxing Crescent", "New"]
        days_of_week.zip(moons) {|day_moon| p day_moon}
        => ["Monday", "New"]
        ["Tuesday", "Waning Crescent"]
        ["Wednesday", "Third Quarter"]
        ["Thursday", "Waning Gibbous"]
        ["Friday", "Full"]
        ["Saturday", "Waxing Gibbous"]
        ["Sunday", "First Quarter"]
        ["Monday", "Waxing Crescent"]
        ["Tuesday", "New"]
        


Now if our moon phases actually worked like that...we may have some problems.