2

__FILE__ returns the path of the current Ruby script file.

One potentially significant problem is that, if using binding.pry, __FILE__ evaluates to (pry). It is potentially problematic to have __FILE__ evaluate to different values depending on whether it is evaluated in the context of binding.pry. For example,

$stdout.print "****************************************\n\n"
$stdout.print "FILE: #{__FILE__}\n\n"
$stdout.print "****************************************\n\n"

binding.pry

When the script pauses at binding.pry, I get:

__FILE__
# >> (pry)

Does anyone know any mechanism to get the path of the current file even in the context of binding.pry?

4
  • 3
    Assign it before stopping. current_file = __FILE__; binding.pry Commented Dec 13, 2018 at 23:53
  • 1
    FYI you don't need to use pry since Ruby 2.4: binding.irb is built-in and largely equivalent. Commented Dec 14, 2018 at 15:33
  • 1
    @Max The same issue presents itself in IRB as in pry. Commented Dec 14, 2018 at 16:54
  • @anothermh yep, my comment is totally unrelated to the question. Just pointing out that pry is no longer necessary Commented Dec 14, 2018 at 16:56

2 Answers 2

2

Use _file_ instead of __FILE__. For example, given two files:

# foo.rb
require 'pry'
require './bar'
binding.pry
b = Bar.new

and:

# bar.rb
require 'pry'
class Bar
  def initialize
    binding.pry
  end
end

Run them with ruby foo.rb:

ruby foo.rb

From: /Users/username/foo.rb @ line 3 :

    1:     require 'pry'
    2:     require './bar'
 => 3:     binding.pry
    4:     b = Bar.new

(main):1 ⇒ _file_
=> "/Users/username/foo.rb"
(main):2 ⇒ exit

From: /Users/username/bar.rb @ line 4 Bar#initialize:

    3: def initialize
 => 4:   binding.pry
    5: end

(#<Bar:0x00007fbb6caaff08>):1 ⇒ _file_
=> "/Users/username/bar.rb"

_file_ and any other local variable names can be found in binding.local_variables.

Sign up to request clarification or add additional context in comments.

Comments

1

Sergio Tulentsev made a simple suggestion, assign __FILE__ to a variable before invoking binding.pry.

anothermh, mentioned _file_ which is available in binding pry.

In the end, I combined the two answers:

# When in the context of binding.pry, __FILE__ resolves to '(pry)',
# binding contains the local variable _file_ which always resolves to
# the current file, even when being evaluated in the context of binding.pry .
# _file_ is only available, in binding. This does the trick:

current_file = __FILE__.downcase == '(pry)' ? _file_ : __FILE__

1 Comment

Beware that _file_ returns the absolute path while __FILE__ does not, so do not expect them to be perfectly interchangeable if you're subsequently parsing the value of current_file. I don't see any reason not to continue using just __FILE__, since the only time I can see _file_ being necessary is when manually typing into the REPL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.