Patch the Monkey

Posted by fakebenjay on June 18, 2017

In programming, monkey patching is the practice of modifying or extending system software within an individual instance of a program (according to Wikipedia). Specifically in languages like Ruby and Python, monkey patching refers to the practice of modifying classes that are built into the language, bending the language’s core functionality to fit your needs. If you want arrays do something that Ruby doesn’t have the capability of doing right out of the box, monkey patching really can come in handy!

And for what it’s worth, monkey patching was originally called “guerilla patching.” Somewhere along the way, that became “gorilla patching,” before being changed to “monkey patching,” probably to make it sound less terrifying.

Like the song from the music video gif above (Peter Gabriel’s “Shock the Monkey”), monkey patching can be super awesome!

However, “Shock the Monkey” is a song about the destructiveness of jealousy, and like the destructiveness of jealousy, monkey patching can be quite dangerous. If you’re messing around with a language’s very DNA, it’s relatively easy to inadvertedly modify something unintentionally. If you monkey patch incorrectly, your program hypothetically might not be able to do something as basic as use arrays, or (less hyperbolically) your program might break easily with future upgrades, as those upgrades make certain assumptions about the language that your monkey patches have undermined.

Flatiron’s only lesson on monkey patching, included in their web development class’ prework, was, for me at the time, incredibly confusing, and seemingly deliberately so. The lesson, which asked students to solve a problem with the Hash class, also introduced the splat operator. For a programming n00b, this was more than enough to scare me away from monkey patching at the time, and just accept it as something weird and spooky and dangerous (or as Peter Gabriel would urge, don’t you monkey with the monkey!)

monkey! monk-ayeee! monkey! DON’T YOU KNOW YOU’VE GOT TO…SHOCK THE MONKEY!!

However, below is a simple, and possibly quite useful example of a monkey patch. Ruby arrays don’t have the ability to average all of their items automatically, but through monkey patching, you can easily give Ruby the ability to calculate multiple kinds of averages. This is particularly useful if you’re the sort of person, who, say, tends to build programs that occasionally use math.

All you have to do is call the Array class, and build the necessary methods inside of it! Here, we’re going to determine the mean of an array’s items (by adding all of them together, and dividing them by the array’s length, and rounding the answer to the nearest hundreth,) the median of that array (by sorting the array and returning the item at the middle of the array, or the item with the index equal to half the array’s length,) and the mode of that array (by counting every item in the array, storing that information in a hash, and returning the number that appears the most often.)

class Array
  def mean
    total = self.inject {|sum, n| sum + n}
    (total.to_f / self.length).round(2)

  def median
    array = self.sort
    array[self.length / 2]

  def mode
    tabulator = {}
    self.each do |a|
      if !tabulator[a]
        tabulator[a] = 1
        tabulator[a] += 1

    sorted = tabulator.sort {|a, b| a[1]<=>b[1]}