Created
September 28, 2017 00:47
-
-
Save ticky/a0e5e52067790cb50f8a1af0dd378a89 to your computer and use it in GitHub Desktop.
Trace all require calls. A late-resort debug option in Ruby 2.x.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pathname' | |
alias :require_original :require | |
def require(*args) | |
called_by = caller_locations(1, 1).first | |
home_relative_path = called_by.path.gsub Dir.home, '~' | |
cwd = Pathname.new(Dir.pwd) | |
cwd_relative_path = Pathname.new(called_by.path).relative_path_from(cwd).to_s | |
shortest_path = if home_relative_path.length > cwd_relative_path.length | |
cwd_relative_path | |
else | |
home_relative_path | |
end | |
STDERR.puts "'#{args.first}' required by #{shortest_path}:#{called_by.lineno}" | |
require_original *args | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment