.reduce
The morning of March 7, 2018 — that’s 21 days ago now —I sat at the edge of a hand-me-down Ikea fold-out chair I had inherited from my older brother. With every nervous movement, each unconscious shift of my weight, the chair would creak and threaten to collapse. The sound unnerved me, because it reminded of why the chair was there in the first place — about 24 hours before that, my Crate & Barrel desk chair (also inherited from my brother) had inexplicably given way under my weight, one of the four legs snapping clear off, and I biffed it from a seated position at my desk onto my bedroom floor.
As ridiculous, and frankly, pathetic, as that image must be, I didn’t have time to laugh at myself. I kicked the chair and it slid five feet back until it hit a heap of laundry. I whipped out that Ikea-runner up and kept chugging away. 24-hours until my official ‘Pre-work Check-In’ and I hadn’t been outside in days.
This ‘Check-In’ was to be the final checkpoint to pass through before I could be cleared to begin at ‘The Flatiron School’ that next week to ‘learn to code’ — bootcamp style. Something I was I had been dreaming about and planning for some time. I had almost finished the assigned pre-work in Ruby, which felt colossal, and with shaking hands, greyed out eyes and a barely-there-am-I-going-to-get-through-this smile waited for my check-in via zoom.
I can remember two things from that much-anticipated meeting:
I remember that getting through and feeling so happy.
I remember .reduce and .inject.

As I walked through some code with an instructor, we scanned through the many lines and read through, piece by piece, and vocalized what was happening through each method, each block. When something didn’t sound quite precise, we would go back and reword to ensure that I was getting the concepts. At the end of the method, the instructor laughed and said that there is a much easier way to go about all of these lines, and make the method more precise while we were at it.
What we needed first was an understanding of the Ruby enumerable: .reduce.

In simple terms, .reduce reduces a collection down to a single result.
It iterates over an array — in my simple example below it will be an array of numbers one by one. The result of that iteration will be used as the accumulator for the next iteration.
Take a look at the accumulator in the syntax above. At the end of the day, the final accumulator will return after .reduce is done iterating through each element of the array it was supplied.

Let’s scoot your chair up to the closest computer and try a simple example! In this case, lets walk through what is happening when we call this method.

When this method is called, this is how it executes:

Wohoo! So we know that the return value is 10, which is the value of the final accumulator after the .reduce has iterated through each and every element of the array.
There are other ways to use write reduce, and it still works! For instance, we can drop the method parameter, and it will automatically start at the first element of your array [0]. Even cooler, we can slam in a symbol into the method parameter with the operation that we would like to accumulate through the array. It would look like this:

My goodness, that is beautiful.
Pretty amazing, really. It looks so simple, but there is a lot more going on here than meets the eye.
My hope is that I continue to learn and grow, the whole business of programing will reduce from its big beautiful mess into something still big, still beautiful, but wonderfully reduced to sophisticated simplicity.
If this is read, thank you. And if this is read by my dear and generous brother, sorry the chair is broken! it’s capsized by my desk and I can’t fix it right now but by June 22nd, I’m going to be pretty great at fixing broken things and we can do it then.