【问题标题】:How can I exit from the Ruby Pry session and continue on the next statement?如何退出 Ruby Pry 会话并继续执行下一条语句?
【发布时间】:2014-06-28 23:05:50
【问题描述】:

我使用 Pry gem (http://pryrepl.org) 来测试我的程序。有时会出现异常,所以我使用这种语法:

require "pry"    
begin
  statement 1
  statement 2 *
  statement 3
rescue => e
  binding.pry
end

当“语句 2”出现异常时,我会在救援条款中得到一个撬动会话。我可以检查我的变量等,“e.backtrace”显示异常的原因,以及导致它的行等。然后我通常在源文件中进行一些更改以避免下次出现异常。但是我不想再次启动我的程序,因为要很长时间才能再次到达“语句 2”。所以我的问题是如何退出 Pry 会话并继续执行“语句 3”的程序?如果我尝试输入“继续”或“下一步”,我只会收到错误消息。

【问题讨论】:

标签: ruby exit next continue pry


【解决方案1】:

所以我的问题是如何退出 Pry 会话并继续我的 关于“声明 3”的程序?

如果 pry-debugger 不适合你,该怎么办:

require "pry"    

begin
  statement 1
  statement 2
rescue => e
  binding.pry
end

begin
  statement 3
rescue => e
  binding.pry
end

例如:

require 'pry'

def a
  puts 'a'
end

def b
  puts 'b'
  nil[1]
end

def c
  puts 'c'
end



begin
  a
  b
rescue => e
  binding.pry
end

begin
  c
rescue => e
  binding.pry
end

--输出:--

~/ruby_programs$ r 1.rb 
a
b

From: /Users/7stud/ruby_programs/1.rb @ line 22 :

    17: 
    18: begin
    19:   a
    20:   b
    21: rescue => e
 => 22:   binding.pry
    23: end
    24: 
    25: begin
    26:   c
    27: rescue => e

[1] pry(main)> exit
c
~/ruby_programs$ 

当然不行,如果你的问题是真的,

如果我的程序在随机行上抛出一个错误,我如何才能窥探到 在下一行继续执行?

...

如果我尝试输入“继续”或“下一步”,我只会收到错误消息。

执行完毕:

require "pry" 

begin
  statement 1
  statement 2 *
  statement 3
rescue => e
  binding.pry   #<== Pry executed here
end
#<=== Next line to execute after rescue clause

【讨论】:

    【解决方案2】:

    我认为在您更改源代码后无法继续执行。我会尝试将 pry 语句靠近语句 2,然后重新运行。

    statement 1
    binding.pry
    statement 2
    statement 3
    

    【讨论】:

      猜你喜欢
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多