如何跳出 Ruby Pry 的循环?
我在 Rails 应用程序中使用 Pry。我在模型的循环内设置了 binding.pry
来尝试调试问题。例如:
(1..100).each do |i|
binding.pry
puts i
end
当我输入 quit
时,它会进入下一次迭代并再次停止。有没有办法跳出循环,这样我就不必输入 quit
100 次?
目前我知道如何摆脱它的唯一方法是使用 CTRL+C 并重新启动应用程序。
I'm using Pry with my Rails application. I set binding.pry
inside a loop in my model to try and debug a problem. For example:
(1..100).each do |i|
binding.pry
puts i
end
When I type quit
, it goes to the next iteration and stops again. Is there a way to step out of the loop so I don't have to type quit
100 times?
Currently the only way I know how to get out of it is to use CTRL+C and restart the application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要无条件退出 Pry,请输入
Edit from @Nick's comment: 也可以:
To exit Pry unconditionally, type
Edit from @Nick's comment: Also works:
我使用:
这将使程序保持运行,但不会继续停止执行。当您在控制台中进行调试时,这特别有用。
I use:
This will keep the program running, but will keep it from continuing to stop execution. This is especially helpful when you are debugging in the console.
要退出所有内容,请使用:
这应该忽略所有正在进行的绑定。
To exit everything, use:
This should ignore all proceeding bindings.
三重感叹号 (
!!!
) 就可以做到这一点。Triple exclamation (
!!!
) would do that.使用
要重新启用,请将其添加到您的控制器
Use
To renable, add this to your controller
binding.pry
语句与 GDB 中的断点完全相同。 GDB 中的此类断点也会被命中 100 次。如果您只想在循环的第一次迭代中命中
binding.pry
一次,请在binding.pry
上使用条件,如下所示:然后退出只需输入
exit
即可退出当前会话。A
binding.pry
statement is exactly the same as a breakpoint in GDB. Such a breakpoint in GDB would be hit 100 times too.If you only want the
binding.pry
to be hit once, for the first iteration of the loop, then use a conditional on thebinding.pry
like so:You then exit the current session by just typing
exit
.按“q”,您将看到就像这种
类型
一样,这个单词会起作用,如果没有:
press 'q' and you will see just like this
type
this one word will do, if not:
基于上面的两个答案:
谢谢你们!你的建议真的对我帮助很大!
我只是想分享一个简单的愚蠢技巧,我个人用它来不用担心
DISABLE_PRY
环境变量。将此回调永久添加到项目的基本控制器ApplicationController
中。每次调用disable-pry
时,它都会自动重新启用 PRY:Based on the two previous answers above:
Thank you guys! Your advices have helped me really a lot!
I just want to share a simple stupid trick, that I personally use to don't worry about the
DISABLE_PRY
environment variable all the time. Add this callback to the base controllerApplicationController
of your project permanently. It would automatically re-enable PRY every time thedisable-pry
is called:使用 gem
pry-moves
您可以使用f
(完成命令)退出循环示例:
Using gem
pry-moves
you can step out of loop usingf
(finish command)example:
如果您只需要调试一次迭代,您可以只引发错误,转义保证:
或使用条件:
If you just need to debug one iteration, you can just raise error, escape guarantee :
Or with condition :