The .sum is greater than the .each of its parts

Jacob Fromm
4 min readSep 8, 2020

I’m a new software engineering student at the Flatiron School, only three weeks into an intense fifteen-week coding bootcamp. Like many other new students, I started coding a few short months before starting this program. And to my surprise, most of that time was spent learning a language (Javascript) I won’t even be using for until much later in the program!

Instead, we’ve been focused on Ruby, a high-level programming language that’s relatively simple to learn and use. In Ruby’s case, however, “simple” certainly doesn’t mean “superficial.” There’s lots to learn about Ruby, and your ability to write robust, clean code depends (partially, at least) on how much of Ruby’s “vocabulary” you know. One of the best ways to build your Ruby vocabulary is to study its many enumerable methods.

Unfortunately for new students, there’s not much time to keep up with the curriculum and memorize methods at the same time. Fortunately, we’ve gotten lots of practice putting many of these methods to use in our labs and assignments.

One such opportunity came during our Mod1 Mock Code Challenge, which prepared us for this module’s assessment—the feared code challenge.

We were tasked with writing a pared-down version of a ride-sharing app to model class associations, including classes for a Passenger, Driver, and Ride. One of the required deliverables for the ride class was a method that found the average length of all total rides. Since this was a timed challenge and I’m a total n00b, I’m relied on familiar with Ruby’s catchall enumerable, #each. In short, the #each method applies whatever code you write in a code block to—you’ll never believe this—each element in an array or a hash.

Each, y’know? For, like, each of ‘em?
Each, y’know? Like, for each of ‘em? See—coding’s super easy!

Now, I’ve historically identified as very-much-not-a-math-person. And despite recent attempts to drop that narrative, when faced with both arithmetic AND a coding challenge, I only have so much bandwith. It’s like parallel parking and listening to music: I can do both well enough, but not at the same time. So I focused on the math and came up with the following code:

a clunky solution to a method that finds the average length of all rides
A truly robust solution…not

So what’s happening here? I set the variable total_dist to 0, and then used the Ruby #each enumerable to increment total_dist by the distance property of each instance of the self class. And since we’re finding the average, I then divided total_dist by the number of total rides. It was a simple solution, which is a nice way of saying inefficient and bad.

As we reviewed our code for the mock challenge, I offered this solution and found that many of my classmates had used different enumerables to find the same solution. In fact, most of them had used two specific methods—#map and #sum—to solve this problem. I’d never used the #sum method before, so I spent some time toying with my code, and here’s where I landed:

getting closer…

What do you know! Instead of going to the trouble of doing all that math (ok, I know it was barely any math at all, but still…ugh), I could’ve used one of Ruby’s built-in methods to do the calculation for me! That’s the cool thing about Ruby enumerables: for most data manipulations you could ever want to do, there’s a method for that! So, to break down what’s happening here: I’m telling the #map method to return an array composed only of each ride instance’s distance property, and then I’m telling Ruby to find the total of those numbers chaining the #sum method to the end of the expression. Unfortunately there’s no method to find the average of a set of numbers, but by dividing the sum by the length of the array (found by #count, another handy method), I was able to find the mean. Now, this is pretty good, but it could be even better:

Hey, look at that! One pretty little line.

Remembering that Ruby returns the last calculated value of a method whether being explicitly told to or not, I’m able to simply write the expression without setting it equal to a variable. I never would’ve gotten to this solution without the creativity and insight of my brilliant classmates, and after learning about their solutions, I was able to experiment with code and think differently about my approach to data manipulation. For a beginner like me, chaining enumerables instead of doing simple calculations can feel like stacking things I don’t know on top of things I don’t know, but that’s a blog post for another time. And with useful enumerables like #map and #sum, I’m not can spend way less time doing all that math—which is good, considering I have a bootcamp to focus on!

--

--