Show full path name of the ruby file when it get loaded

Show a file's pathname in Ruby

How about simple (from a quicky test in Camping):

File.expand_path(File.dirname(__FILE__))

?


The best way I can think of is to patch the Kernel module. According to the docs Kernel::load searches $: for the filename. We can do the same thing and print the path if we find it.

module Kernel
  def load_and_print(string)
    $:.each do |p|
      if File.exists? File.join(p, string)
        puts File.join(p, string)
        break
      end
    end
    load_original(string)
  end

  alias_method :load_original, :load
  alias_method :load, :load_and_print

end

We use alias_method to store the original load method, which we call at the end of ours.

Tags:

Ruby