Lord Enumerable

All Hail the Enumerable Methods!

March 21, 2015

The Enumerable Method. Sounds rather grand, doesn't it? Perhaps an epic? In this week's blog we will be discussing what an enumerable method is, and specifically, what #cycle is. Enumerable methods are quite the powerful tool in Ruby. They are enable to interact on multiple levels with any object that acts as a collection. For instance, a string, an array, and a hash are all able to be given up as offerings to the Lord Enumerable. As a rule, any object that can be modified with #each can interact with an Enumerable. "How do they work?" you ask? Well, lets look at #cycle.

Cycle does what it says it does. It cycles whatever you give it. Ok, more specifically, it will examine what you input and perform whatever actions are specified in the block. Let's just say that I've upset my girlfriend(hypothetically, of course) and I want to tell her how sorry I am. I could write a program that would tell her. She'd like that :). I could write this.

message=" I'm Really Sorry!"
5.times {|i| puts message}
end

That will tell her i'm sorry five times. Solid apology, but more effort than necessary. Please don't tell her that btw... To make things easier, I could use cycle.

message.cycle(10) {|i| puts message}

This will return the message ten times. A better apology, and much easier. The only risks of the Enumerable #cycle are not specifying a cycle count and not using the block. The lack of a cycle(argument) will return the block infinite times, or until the computer is told to stop. The lack of a block will return an Enumerator. Nobody really knows what that means. It's a mystery. If I had to guess I'd say that it would be the contents of the object and the name of the method...so
=> "I'm Really Sorry!":cycle.

Enumerables are awesome. Use them or else....well, just use them.

Again Soon,
Staunton