如何跳出 Ruby Pry 的循环?

发布于 2024-12-13 18:48:33 字数 309 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

貪欢 2024-12-20 18:48:33

要无条件退出 Pry,请输入

exit-program

Edit from @Nick's comment: 也可以:

!!!

To exit Pry unconditionally, type

exit-program

Edit from @Nick's comment: Also works:

!!!
家住魔仙堡 2024-12-20 18:48:33

我使用:

disable-pry

这将使程序保持运行,但不会继续停止执行。当您在控制台中进行调试时,这特别有用。

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.

眼睛会笑 2024-12-20 18:48:33

要退出所有内容,请使用:

exit!

这应该忽略所有正在进行的绑定。

To exit everything, use:

exit!

This should ignore all proceeding bindings.

油饼 2024-12-20 18:48:33

三重感叹号 (!!!) 就可以做到这一点。

Triple exclamation (!!!) would do that.

ら栖息 2024-12-20 18:48:33

使用

disable-pry

要重新启用,请将其添加到您的控制器

ENV['DISABLE_PRY'] = nil

Use

disable-pry

To renable, add this to your controller

ENV['DISABLE_PRY'] = nil
疯狂的代价 2024-12-20 18:48:33

binding.pry 语句与 GDB 中的断点完全相同。 GDB 中的此类断点也会被命中 100 次。

如果您只想在循环的第一次迭代中命中 binding.pry 一次,请在 binding.pry 上使用条件,如下所示:

(1..100).each do |i|
  binding.pry if i == 1
  puts i
end

然后退出只需输入 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 the binding.pry like so:

(1..100).each do |i|
  binding.pry if i == 1
  puts i
end

You then exit the current session by just typing exit.

养猫人 2024-12-20 18:48:33

按“q”,您将看到就像这种

[1] pry(#<AlbumsController>)>

类型

exit

一样,这个单词会起作用,如果没有:

control + c

press 'q' and you will see just like this

[1] pry(#<AlbumsController>)>

type

exit

this one word will do, if not:

control + c
岁月染过的梦 2024-12-20 18:48:33

基于上面的两个答案:

谢谢你们!你的建议真的对我帮助很大!

我只是想分享一个简单的愚蠢技巧,我个人用它来不用担心 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

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 controller ApplicationController of your project permanently. It would automatically re-enable PRY every time the disable-pry is called:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :reenable_pry

  private

  def reenable_pry
    ENV['DISABLE_PRY'] = nil
  end
end
夏九 2024-12-20 18:48:33

使用 gem pry-moves 您可以使用 f (完成命令)退出循环


示例:

    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

Using gem pry-moves you can step out of loop using f (finish command)


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
山人契 2024-12-20 18:48:33

如果您只需要调试一次迭代,您可以只引发错误,转义保证:

(1..100).each do |i|
  binding.pry
  raise
  puts i
end

或使用条件:

(1..100).each do |i|
  if i == 50
    binding.pry 
    raise
  end
  puts i
end

If you just need to debug one iteration, you can just raise error, escape guarantee :

(1..100).each do |i|
  binding.pry
  raise
  puts i
end

Or with condition :

(1..100).each do |i|
  if i == 50
    binding.pry 
    raise
  end
  puts i
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文