9

在 Ruby 脚本中加载 Pry REPL 我得到了这个奇怪的错误:

before_session hook failed: Pry::CommandError: Cannot locate this method: load.
~/.rvm/gems/ruby-2.0.0-p195/gems/pry-0.9.12.2/lib/pry/method.rb:498:in `pry_doc_info'
(see _pry_.hooks.errors to debug)

知道问题是什么吗?

注意: 1. 除了那个神秘的消息之外,代码似乎执行得很好; 2. 我找不到“ _pry_.hooks.errors”文件

4

2 回答 2

1

我在 Gemfile 中使用 Ruby 2.4.1 和 Pry Stack Explorer 遇到了这个问题:

gem 'pry'
gem 'pry-rescue'
gem 'pry-stack_explorer'

在插入 Pry 调试器时,我看到:

before_session hook failed: Pry::CommandError: Cannot locate this method: load. Invoke the 'gem-install pry-doc' Pry command to get access to Ruby Core documentation.
/Users/alexharvey/.rvm/gems/ruby-2.4.1/gems/pry-0.11.3/lib/pry/method.rb:489:in `pry_doc_info'
(see _pry_.hooks.errors to debug)

然后我尝试按照有关.hooks.errors 的说明进行操作:

[2] pry(#<MarkdownLint::Rule>)> puts _pry_.hooks.errors
Cannot locate this method: load. Invoke the 'gem-install pry-doc' Pry command to get access to Ruby Core documentation.
=> nil

所以我只是将 pry-doc 添加到我的 Gemfile 中。然后,我还有另一个问题。在尝试退出调试器时:

[2] pry(#<MarkdownLint::Rule>)>
when_started hook failed: NameError: uninitialized constant RubyVM::DebugInspector
/Users/alexharvey/.rvm/gems/ruby-2.4.1/gems/binding_of_caller-0.8.0/lib/binding_of_caller/mri2.rb:21:in `callers'
(see _pry_.hooks.errors to debug)

我发现我可以通过请求不是最新版本的 debug_inspector 来解决这个问题。

最后,为了成功使用 pry 和 pry-stack_explorer,我得到了:

gem 'pry'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-doc'
gem 'debug_inspector', '<= 0.0.2'
于 2018-09-15T11:24:55.280 回答
0

从源头看来,钩子可能引发了异常,但随后将其吞下。上面的评论exec_hook建议您进行询问$pry_hook_error以了解发生了什么。

# Execute the specified hook.
# @param [Symbol] name The hook name to execute
# @param [*Object] args The arguments to pass to the hook
# @return [Object, Exception] The return value of the hook or the exception raised
#
# If executing a hook raises an exception, we log that and then continue sucessfully.
# To debug such errors, use the global variable $pry_hook_error, which is set as a
# result.
def exec_hook(name, *args, &block)
  e_before = hooks.errors.size
  hooks.exec_hook(name, *args, &block).tap do
    hooks.errors[e_before..-1].each do |e|
      output.puts "#{name} hook failed: #{e.class}: #{e.message}"
      output.puts "#{e.backtrace.first}"
      output.puts "(see _pry_.hooks.errors to debug)"
    end
  end
end

我无法重现这个,所以如果这非常离谱,请原谅我。

于 2015-02-26T02:38:21.853 回答