hunting monkey patches

August 08, 2015

Ruby monkeypatching is a dangerous, but necessary tool. It is convenient to add new behaviours to existing classes, or to replace existing methods with more customized or secure code. Once the patch is in place, however, it is easy to forget that it is there.

Recently, I was trying to discern the origin of such a patch, and discovered a new (to me) function in pry, my favourite Ruby debugger: show-source.

  1. Put a binding pry below the monkeypatch so that it can be caught at boot time
  2. Use cd to change directories into the reopened class
  3. Use show-source to find the exact location where the method was originally defined
  4. Use show-source -a to show the source and location of EVERY MONKEYPATCH

In action:

1. Debug your code at boot time

step1 pry

2. Reopen the class

step2 cd

3. Use ‘show-source’

step3 show source

4. Use ‘show-source -a’

step4 show source a1 step4 show source a2

Katie Leonard

Mostly Katie explaining things to herself.

© 2025