Today I Learned

hashrocket A Hashrocket project

Uncover monkey patched methods

Due to Ruby's open classes, anyone can monkey patch any thing at any time, which could lead to confusion if you aren't aware of the patched methods. You can check a method to see if it's been monkey patched in irb/pry/byebug without exiting and grepping the source code.

class Array
  def length
arr = []
suspect_method  = arr.method :length
method = arr.method :size

Here we create some references to the length and size methods on Array. When you call source_location on one of these, if the method is defined in native Ruby, it will return nil, if it has been monkey patched, it will return the file name and line number where it was defined.

# ['some_file.rb', 2]

See More #ruby TILs
Looking for help? Each developer at Hashrocket has years of experience working with Ruby applications of all types and sizes. We're an active presence at Ruby conferences, have written some of the most popular gems, and have worked on many of the web's Ruby on Rails success stories. Contact us today to talk about your Ruby project.