如何使用 Ruby Pry 跳出循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8015531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I step out of a loop with Ruby Pry?
提问by Ryan
I'm using Pry with my Rails application. I set binding.pryinside a loop in my model to try and debug a problem. For example:
我在 Rails 应用程序中使用 Pry。我binding.pry在模型中设置了一个循环来尝试调试问题。例如:
(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 quit100 times?
当我输入时quit,它会进入下一次迭代并再次停止。有没有办法跳出循环,这样我就不必输入quit100 次了?
Currently the only way I know how to get out of it is to use CTRL+Cand restart the application.
目前我知道如何摆脱它的唯一方法是使用CTRL+C并重新启动应用程序。
回答by Evandro
To exit Pry unconditionally, type
要无条件退出 Pry,请键入
exit-program
Edit from @Nick's comment: Also works:
从@Nick 的评论中编辑:也有效:
!!!
回答by stebooks
I use:
我用:
disable-pry
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.
这将保持程序运行,但会阻止它继续停止执行。这在您在控制台中调试时特别有用。
回答by Blake
To exit everything, use:
要退出所有内容,请使用:
exit!
This should ignore all proceeding bindings.
这应该忽略所有正在进行的绑定。
回答by Hahn
Triple exclamation (!!!) would do that.
三重感叹号 ( !!!) 会做到这一点。
回答by Chet3x16
Use
用
disable-pry
To renable, add this to your controller
要重新启用,请将其添加到您的控制器
ENV['DISABLE_PRY'] = nil
回答by horseyguy
A binding.prystatement is exactly the same as a breakpoint in GDB. Such a breakpoint in GDB would be hit 100 times too.
一个binding.pry说法是完全一样的GDB断点。GDB 中这样的断点也会被击中 100 次。
If you only want the binding.pryto be hit once, for the first iteration of the loop, then use a conditional on the binding.prylike so:
如果您只想binding.pry命中一次,对于循环的第一次迭代,则使用binding.pry类似这样的条件:
(1..100).each do |i|
binding.pry if i == 1
puts i
end
You then exit the current session by just typing exit.
然后,您只需键入 即可退出当前会话exit。
回答by zinovyev
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_PRYenvironment variable all the time. Add this callback to the base controller ApplicationControllerof your project permanently. It would automatically re-enable PRY every time the disable-pryis called:
我只想分享一个简单的愚蠢技巧,我个人使用它来不用一直担心DISABLE_PRY环境变量。将此回调ApplicationController永久添加到项目的基本控制器。每次disable-pry调用时它都会自动重新启用 PRY :
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
before_action :reenable_pry
private
def reenable_pry
ENV['DISABLE_PRY'] = nil
end
end
回答by Daniel Garmoshka
Using gem pry-movesyou can step out of loop using f(finish command)
使用 gempry-moves您可以使用f(finish 命令)跳出循环
example:
例子:
42: def test
43: 3.times do |i|
=> 44: binding.pry
45: puts i
46: end
47: puts :finish
48: end
[1] pry(main)> f
0
1
2
Frame: 0/1 method
From: playground/sand.rb:47 main
42: def test
43: 3.times do |i|
44: binding.pry
45: puts i
46: end
=> 47: puts :finish
48: end
回答by Heartless Vayne
press 'q' and you will see just like this
按'q',你会看到这样
[1] pry(#<AlbumsController>)>
type
类型
exit
this one word will do, if not:
如果没有,这个词就可以:
control + c

