tags:

views:

1322

answers:

3

When reading source code, I always want to know the full path of the file when it is loaded, is there any callback method in ruby to accomplish this, or any other way to do this? thanks in advance.

EDIT Clarification from the comments:

I want to know where the loaded "somefile" is located while I execute this line: "load somefile"

+8  A: 

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.

Gordon Wilson
This is incorrect. __FILE__ contains the name the script was called as and as such will only contain the full path name if the script was called using the full path name.
Robert Gamble
Yes, I agree with Robert. What I want is when the code line "load somefile" is executed, the full path of somefile.rb is printed.
eric2323223
@Robert, oops you're quite right. Answer is edited with a correct solution.
Gordon Wilson
This won't work either; sometimes you will get the right answer, sometimes you will get the wrong answer, sometimes you won't get an answer at all.
Robert Gamble
I still don't quite understand. The __FILE__, as I understand, should be the current file name, not the file I am loading. Could you please be more specific?
eric2323223
If you are looking for the name of a loaded module, using __FILE__ in the file loading the module isn't going to work. Also, the Pathname trick will only work if the file exists in the current working directory as that is that the example does, it just prepends the cwd to the filename provided.
Robert Gamble
@Robert, Re:"This won't work either": Works for me in OS X, Vista, and Red Hat, using ruby 1.8.6. Would you mind providing a reference or a reliable solution? @eric, perhaps I've misunderstood the question. You want "require 'somefile.rb'" to print the path of 'somefile.rb' to std out?
Gordon Wilson
@Grodon, yes, I want to know where the loaded somefile.rb is located while I execute this line: "load somefile".
eric2323223
@Gordon: It worked for you because your test cases were very limited. If you don't provide an absolute path to Pathname it will assume it is in your cwd, if it isn't it won't work. You can easily confirm this by doing "Pathname.new('blah')" and examining the resulting exception.
Robert Gamble
I am not aware of a reliable solution (which doesn't mean there isn't one, I haven't researched the question), if I was I would have posted it.
Robert Gamble
@Robert, make sense. Thanks for the explanation.
Gordon Wilson
@Grodon, The method alias is cool, thanks:)
eric2323223
I made a mistake, the "load" method will load the file with the name exactly same as its input parameter, so the "load somefile" is actually loading file "somefile", not "somefile.rb".
eric2323223
A: 

I'm curious why you're using load instead of require - in all of my time using Ruby. I've never had occasion to utilize load.

elliottcable
In fact, I asked this question for my code-reading of ZenTest, which include some lines using "load somefile"
eric2323223
Ah, understood.If you're looking to learn Ruby via reading, may I suggest Merb? It's got some extremely well documented source code, and follows many best practices. The same can't really be said for ZenTest - after all, Zen<filtered> did write it!
elliottcable
+4  A: 

How about (from a quicky test in Camping):

File.expand_path(File.dirname(__FILE__))

?

Dave Everitt