gpt4 book ai didi

ruby - 使用 pry-byebug 设置条件断点

转载 作者:数据小太阳 更新时间:2023-10-29 07:26:39 28 4
gpt4 key购买 nike

给定以下代码:

require 'pry-byebug'

10.times.with_index do |i|
binding.pry
puts i
end

我想在 pry-byebug 中“循环直到 i == 5 然后中断”。通过运行它 help break,您似乎可以通过“breakpoint #”或“line #”来识别断点。看起来你也可以使用条件,但我没有让它工作:

尝试在第 # 行设置断点:

$  ruby foo.rb

From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :

1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end

[1] pry(main)> break foo.rb:5 if i == 5

Breakpoint 1: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ 5 (Enabled) Condition: i == 5


2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end


[2] pry(main)> continue
0

From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :

1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end

[2] pry(main)> i
=> 1

首先设置断点,然后在断点上放置条件:

➜  ~/D/w/t/pry_debug  ruby foo.rb

From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :

1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end

[1] pry(main)> break foo.rb:15

Breakpoint 1: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ 15 (Enabled)



[2] pry(main)> break --condition 1 i == 5
[3] pry(main)> c
0

From: /Users/max/Dropbox/work/tmp/pry_debug/foo.rb @ line 5 :

1: require 'pry-byebug'
2:
3: 10.times.with_index do |i|
4: binding.pry
=> 5: puts i
6: end

[3] pry(main)> i
=> 1

如您所见,在这两种情况下,pry-byebug 都不遵守条件,因为它停止得太早了。如何让它发挥作用?

最佳答案

我认为一种简单的方法是在您的代码中执行此操作:

require 'pry-byebug'

10.times.with_index do |i|
binding.pry if i == 5
puts i
end

或者如果你想在异常时停止,你可以这样做:

require 'pry-byebug'

10.times.with_index do |i|
puts i rescue binding.pry # if the puts command throws an exception, you'll be taken to pry
end

关于ruby - 使用 pry-byebug 设置条件断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41474811/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com