Skip to content

Instantly share code, notes, and snippets.

@ticky
Created September 28, 2017 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ticky/a0e5e52067790cb50f8a1af0dd378a89 to your computer and use it in GitHub Desktop.
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.
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