Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby __使用binding.pry时,文件返回不同的值_Ruby_Pry_Binding.pry - Fatal编程技术网

Ruby __使用binding.pry时,文件返回不同的值

Ruby __使用binding.pry时,文件返回不同的值,ruby,pry,binding.pry,Ruby,Pry,Binding.pry,返回当前Ruby脚本文件的路径 一个潜在的重大问题是,如果使用binding.pry,\uuuuuuuuuuuuuuu文件计算结果为(pry)。根据是否在binding.pry的上下文中对其求值,将\uuuuuuuuuuuuuuuuuuuu文件求值为不同的值可能会有问题。比如说, $stdout.print "****************************************\n\n" $stdout.print "FILE: #{__FILE__}\n\n" $stdout.pr

返回当前Ruby脚本文件的路径

一个潜在的重大问题是,如果使用
binding.pry
\uuuuuuuuuuuuuuu文件计算结果为
(pry)
。根据是否在
binding.pry
的上下文中对其求值,将
\uuuuuuuuuuuuuuuuuuuu文件
求值为不同的值可能会有问题。比如说,

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

binding.pry
当脚本在binding.pry处暂停时,我得到:

__FILE__
# >> (pry)

是否有人知道任何机制来获取当前文件的路径,即使是在
绑定.pry
的上下文中?

使用
\u文件
而不是
\u文件
。例如,给定两个文件:

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

使用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"
ruby foo.rb
From:/Users/username/foo.rb@第3行:
1:需要“撬动”
2:需要“./bar”
=>3:binding.pry
4:b=Bar.new
(主要内容):1⇒ _文件_
=>“/Users/username/foo.rb”
(主要):2⇒ 出口
From:/Users/username/bar.rb@line 4 bar#初始化:
3:def初始化
=>4:binding.pry
5:完
(#):1 ⇒ _文件_
=>“/Users/username/bar.rb”

可以在中找到
\u文件
和任何其他局部变量名。

Sergio Tulentsev提出了一个简单的建议,在调用
binding.pry之前,将
\u文件
分配给变量

anothermh,提到了binding pry中提供的
\u文件

最后,我结合了两个答案:

# 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__

在停止之前分配它<代码>当前文件=\uuuuu文件\uuuuuu;binding.pry
FYI您不需要使用
pry
,因为Ruby 2.4:
binding.irb
是内置的,基本上是等效的。@Max在irb中出现的问题与pry相同。@Anotherh yep,我的评论与这个问题完全无关。只需指出,
pry
不再是必需的。请注意,
\u文件
返回绝对路径,而
\u文件
不返回绝对路径,因此,如果随后解析
当前文件
的值,请不要期望它们可以完全互换。我看不出有任何理由不继续只使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,因为我唯一能看到
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
# 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__