Can I drop to an IRB prompt from a running Ruby script?
我可以从运行的Ruby脚本中切换到IRB提示符吗?
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.
我想运行一个脚本,但是让它在程序的某个点上给我一个IRB提示符,提示符是程序的当前状态,而不是仅仅通过运行rdebug和断点。
6 个解决方案
#1
27
you can use ruby-debug to get access to irb
您可以使用ruby-debug来访问irb
require 'rubygems'
require 'ruby-debug'
x = 23
puts "welcome"
debugger
puts "end"
when program reaches debugger you will get access to irb.
当程序到达调试器时,您将访问irb。
#2
44
Pry (an IRB alternative) also lets you do this, in fact it was designed from the ground up for exactly this use case :)
Pry (IRB的一种替代方案)也允许您这样做,事实上它是为这个用例从头开始设计的:)
It's as easy as putting binding.pry
at the point you want to start the session:
它和装订一样简单。在你想要开始会议的时候:
require 'pry'
x = 10
binding.pry
And inside the session:
并在会话:
pry(main)> puts x
=> 10
Check out the website: http://pry.github.com
看看这个网站:http://pry.github.com
Pry let's you:
撬让你:
- drop into a session at any point in your code
- 在您的代码中的任何位置插入会话
- view method source code
- 视图方法的源代码
- view method documentation (not using RI so you dont have to pre-generate it)
- 查看方法文档(不使用RI,所以您不必预先生成它)
- pop in and out of different contexts
- 在不同的上下文中弹出
- syntax highlighting
- 语法高亮显示
- gist integration
- 依据集成
- view and replay history
- 视图和重演历史
- open editors to edit methods using
edit obj.my_method
syntax - 打开编辑器,使用edit obj编辑方法。my_method语法
A tonne more great and original features
更多伟大和原创的特征
#3
14
apparently it requires a chunk of code to drop into irb.
显然,它需要一段代码才能进入irb。
Here's the link (seems to work well).
这是链接(看起来效果不错)。
http://jameskilton.com/2009/04/02/embedding-irb-into-your-ruby-application
http://jameskilton.com/2009/04/02/embedding-irb-into-your-ruby-application
require 'irb'
module IRB
def self.start_session(binding) # call this method to drop into irb
unless @__initialized
args = ARGV
ARGV.replace(ARGV.dup)
IRB.setup(nil)
ARGV.replace(args)
@__initialized = true
end
workspace = WorkSpace.new(binding)
irb = Irb.new(workspace)
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
@CONF[:MAIN_CONTEXT] = irb.context
catch(:IRB_EXIT) do
irb.eval_input
end
end
end
#4
8
This feature is available from Ruby 2.4. You can just use binding.irb
这个特性可以从Ruby 2.4中获得。你可以用binding。irb
E.g.
如。
require 'irb'
a = 10
binding.irb
puts a
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.
如果您运行上述代码,您将获得irb控制台,以便您可以检查本地变量的值和范围内的任何其他值。
Source: http://blog.redpanthers.co/new-binding-irb-introduced-ruby-2-4/
来源:http://blog.redpanthers.co/new-binding-irb-introduced-ruby-2-4/
Ruby commit: https://github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a
Ruby提交:https://github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a
#5
0
Just add this line to where you want the breakpoint:
只需在需要断点的地方添加这一行:
require 'ruby-debug';debugger
需要“ruby-debug”;调试器
but i suggest use pry instead of irb, which is super handy, insert the following line instead:
但是我建议用pry代替irb,这是非常方便的,插入下面的一行代替:
require 'pry'; binding.pry
需要“撬”;binding.pry
#6
0
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.
我已经很晚了,但是如果你已经在irb/pry中加载了一个脚本,一个简单的raise也可以让你回到irb/pry提示符。当我在rails控制台中编写一个脱机脚本时,我经常使用这种方法。
#1
27
you can use ruby-debug to get access to irb
您可以使用ruby-debug来访问irb
require 'rubygems'
require 'ruby-debug'
x = 23
puts "welcome"
debugger
puts "end"
when program reaches debugger you will get access to irb.
当程序到达调试器时,您将访问irb。
#2
44
Pry (an IRB alternative) also lets you do this, in fact it was designed from the ground up for exactly this use case :)
Pry (IRB的一种替代方案)也允许您这样做,事实上它是为这个用例从头开始设计的:)
It's as easy as putting binding.pry
at the point you want to start the session:
它和装订一样简单。在你想要开始会议的时候:
require 'pry'
x = 10
binding.pry
And inside the session:
并在会话:
pry(main)> puts x
=> 10
Check out the website: http://pry.github.com
看看这个网站:http://pry.github.com
Pry let's you:
撬让你:
- drop into a session at any point in your code
- 在您的代码中的任何位置插入会话
- view method source code
- 视图方法的源代码
- view method documentation (not using RI so you dont have to pre-generate it)
- 查看方法文档(不使用RI,所以您不必预先生成它)
- pop in and out of different contexts
- 在不同的上下文中弹出
- syntax highlighting
- 语法高亮显示
- gist integration
- 依据集成
- view and replay history
- 视图和重演历史
- open editors to edit methods using
edit obj.my_method
syntax - 打开编辑器,使用edit obj编辑方法。my_method语法
A tonne more great and original features
更多伟大和原创的特征
#3
14
apparently it requires a chunk of code to drop into irb.
显然,它需要一段代码才能进入irb。
Here's the link (seems to work well).
这是链接(看起来效果不错)。
http://jameskilton.com/2009/04/02/embedding-irb-into-your-ruby-application
http://jameskilton.com/2009/04/02/embedding-irb-into-your-ruby-application
require 'irb'
module IRB
def self.start_session(binding) # call this method to drop into irb
unless @__initialized
args = ARGV
ARGV.replace(ARGV.dup)
IRB.setup(nil)
ARGV.replace(args)
@__initialized = true
end
workspace = WorkSpace.new(binding)
irb = Irb.new(workspace)
@CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
@CONF[:MAIN_CONTEXT] = irb.context
catch(:IRB_EXIT) do
irb.eval_input
end
end
end
#4
8
This feature is available from Ruby 2.4. You can just use binding.irb
这个特性可以从Ruby 2.4中获得。你可以用binding。irb
E.g.
如。
require 'irb'
a = 10
binding.irb
puts a
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.
如果您运行上述代码,您将获得irb控制台,以便您可以检查本地变量的值和范围内的任何其他值。
Source: http://blog.redpanthers.co/new-binding-irb-introduced-ruby-2-4/
来源:http://blog.redpanthers.co/new-binding-irb-introduced-ruby-2-4/
Ruby commit: https://github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a
Ruby提交:https://github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a
#5
0
Just add this line to where you want the breakpoint:
只需在需要断点的地方添加这一行:
require 'ruby-debug';debugger
需要“ruby-debug”;调试器
but i suggest use pry instead of irb, which is super handy, insert the following line instead:
但是我建议用pry代替irb,这是非常方便的,插入下面的一行代替:
require 'pry'; binding.pry
需要“撬”;binding.pry
#6
0
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.
我已经很晚了,但是如果你已经在irb/pry中加载了一个脚本,一个简单的raise也可以让你回到irb/pry提示符。当我在rails控制台中编写一个脱机脚本时,我经常使用这种方法。