如何从正在运行的脚本转到 IRB 提示符?
我可以从正在运行的 Ruby 脚本切换到 IRB 提示符吗?
我想运行一个脚本,但然后让它在程序中的某个点上给我一个 IRB 提示以及程序的当前状态,但不仅仅是通过运行 rdebug 并设置断点。
Can I drop to an IRB prompt from a running Ruby script?
I want to run a script, but then have it give me an IRB prompt at a point in the program with the current state of the program, but not just by running rdebug and having a breakpoint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Pry(IRB 替代方案)也可以让您执行此操作,事实上它是专门为这种用途而设计的case :)
只需将
binding.pry
放在要启动会话的位置即可:在会话内部:
查看网站:http://pry.github.com
撬动您:
edit obj.my_method
语法编辑方法一吨更多伟大和原始的功能
Pry (an IRB alternative) also lets you do this, in fact it was designed from the ground up for exactly this use case :)
It's as easy as putting
binding.pry
at the point you want to start the session:And inside the session:
Check out the website: http://pry.github.com
Pry let's you:
edit obj.my_method
syntaxA tonne more great and original features
您可以使用 ruby-debug 来访问 irb,
当程序到达调试器时,您将可以访问 irb。
you can use ruby-debug to get access to irb
when program reaches debugger you will get access to irb.
显然它需要一大块代码才能放入 irb 中。
这是链接(似乎效果很好)。
http://jameskilton.com/2009/04/ 02/将 irb 嵌入到您的 ruby 应用程序中
apparently it requires a chunk of code to drop into irb.
Here's the link (seems to work well).
http://jameskilton.com/2009/04/02/embedding-irb-into-your-ruby-application
该功能从 Ruby 2.4 开始提供。 您可以只使用
binding.irb
例如,
如果您运行上面的代码,您将获得 irb 控制台,以便您可以检查局部变量的值以及范围内的任何其他内容。
资料来源:http://blog.redpanthers.co/new- binding-irb-introduced-ruby-2-4/
Ruby 提交: https:// github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a
This feature is available from Ruby 2.4. You can just use
binding.irb
E.g.
If you run above code, you will get irb console, so that you can inspect values of local variables and anything else that is in scope.
Source: http://blog.redpanthers.co/new-binding-irb-introduced-ruby-2-4/
Ruby commit: https://github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a
只需将此行添加到您想要断点的位置:
require 'ruby-debug';debugger
但我建议使用 pry 而不是 irb,这非常方便,请插入以下行:
require '撬'; 绑定.pry
Just add this line to where you want the breakpoint:
require 'ruby-debug';debugger
but i suggest use pry instead of irb, which is super handy, insert the following line instead:
require 'pry'; binding.pry
我已经很晚了,但是如果您已经从 irb/pry 中加载脚本,一个简单的
raise
也可以让您返回 irb/pry 提示符。 在 Rails 控制台中编写一次性脚本时,我经常使用这一点。I'm quite late to the game but if you're loading a script from within irb/pry already, a simple
raise
also works to pop you back out to the irb/pry prompt. I use this quite often when writing one off scripts within the rails console.