9

Apologies for the poorly worded question title - no idea how to put it better!

In the following code, when I execute ruby bar.rb, how can I make it output bar.rb, rather than foo.rb?

In foo.rb:

module Foo
  def filename
    __FILE__
  end
end

In bar.rb:

require_relative 'foo'
include Foo

puts filename # outputs 'foo.rb'

This is for a library function that, each time some code is executed, records the location (and git ref) of that code.

7
  • 3
    This is utterly hackish, but the caller method can get you some of the way there. def filename; file = caller[0].split(':'); file[0]; end.
    – Telemachus
    Oct 15, 2012 at 11:56
  • hah :) I wouldn't be surprised if that was the only solution.
    – seb
    Oct 15, 2012 at 12:02
  • It may be, but it still feels hacky as hell. Having to parse the output that way is just foul. caller returns an array of strings, and the first part of each string is what you want. Depending on what you're doing, you may need to consider where in the stack you want to look though. That is, you may not always want the first entry in the array.
    – Telemachus
    Oct 15, 2012 at 12:07
  • 1
    $0 is the name of the file used to start the program. so it will return the value of file_path.rb in ruby <file_path.rb>. Search for '$0' in ruby-lang.org/en/documentation/quickstart/4
    – deepak
    Oct 15, 2012 at 12:35
  • 6
    Use caller if you want to find the filename of the caller of the method. Use $0 if you want to find the executable. Use caller inside def self.included if you want to find the filename when someone includes the module. Oct 15, 2012 at 12:41

2 Answers 2

4

Your question stimulated me to crack open the Ruby interpreter source and see how __FILE__ actually works. The answer is pretty interesting: it's implemented right inside the parser. The lexer has a special token type for __FILE__. When the parser sees that token, it converts it to a string constant, which contains the name of the file the parser is working on.

From line 14948 of ext/ripper/ripper.c:

case keyword__FILE__:
return NEW_STR(rb_external_str_new_with_enc(ruby_sourcefile, strlen(ruby_sourcefile),
                        rb_filesystem_encoding()));

I think this should make it clear that trying to make __FILE__ return the name of the including file is completely impossible, unless you hack the Ruby interpreter source, or write your own preprocessor which transforms __FILE__ to something else before passing the Ruby source to the interpreter!

1

There is a trick you might be a able to use. If you pass a block to the method you could use the blocks closure to determine it's source. Something like:

def filename(&blk)
  blk.eval "__FILE__"
end

But again, that means you have to pass a block.

Honestly I wonder what you are trying to accomplish, b/c outside of make some common core extension method, this is probably something you really don't want to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.