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
"MONKEY PATCHED!"
end
end
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.
suspect_method.source_location
# ['some_file.rb', 2]
method.source_location
#nil
Tweet