The Each Method

| Comments

Today marks day 9 into my 60 day course at the Flatiron School. Essentially, I’m 15% done. I would best summarize the last 9 days with these simple words: anxiety, frustration, and (sometimes) victory.

The thing about this course, essentially a “full-immersion language course”, is the pace in which things are taught. It’s fast. I was prepared for fast, but not this fast. We’ve been told from day one, “If you don’t understand something now, don’t worry. You will use these methods for the rest of your life, and at some point, they will make sense.” I’m not used to moving on to new material before I grasp the current material, but we’ve also been told to trust the structure of the program, so I have.

So each day has been filled with new material, and thus new frustrations. A day hasn’t gone by where I’ve been utterly and completely lost. I don’t want to define myself as the potential crier for the class, but….I could be.

Yesterday, after spending close to three hours on homework, I called it a night. And then something happened, a moment of realization. I UNDERSTAND ITERATIONS. Two days prior, literally a mere 48 hours, iterations were some mystical thing that I was struggling to grasp. And without even realizing it, I had been using the each method with ease in my programs. AND I DIDN’T EVEN REALIZE THAT I UNDERSTOOD THEM. The pace of the class kept me from noticing when material had sunk in.

And with that introduction, I give you the each method.

Say you have an array of candy:

Image of and array containing reeces, twizzlers, mambas and snickers

The each method iterates, or loops, through each item in an array. In layman’s terms, the method acts as a pointer and moves through the array, first to “reeces”, then to “twizzlers”, then “mambas”, and finally to “snickers”. Objects that can be iterated through are called enumerables. In Ruby, strings, hashes, and arrays are all enumerators.

Once the array has been defined, we can implement the each method on that array. The each method only returns the original array, meaning all it does is move through each index. Nothing is actually done to or with the data, and no new array is created. Image of and array containing reeces, twizzlers, mambas and snickers

However, you can do things like this:

Image of and array containing reeces, twizzlers, mambas and snickers

Here I added a puts in my block of code. The |candy| refers to each type of candy included my array. In my string, I refer back to each individual instance of candy, which then includes it in the string. But take note of the actual return value from Ruby, it is still the original array. Again, the each method does not do anything to the original array, it simply takes stock of each item in the array as it passes through and implements the code included in the block (which here is to puts my string).

Comments