How do I drop to the IRB prompt from a running script? How do I drop to the IRB prompt from a running script? ruby ruby

How do I drop to the IRB prompt from a running script?


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:

require 'pry'x = 10binding.pry

And inside the session:

pry(main)> puts x=> 10

Check out the website: 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)
  • 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

A tonne more great and original features


you can use ruby-debug to get access to irb

require 'rubygems'require 'ruby-debug'x = 23puts "welcome"debuggerputs "end"

when program reaches debugger you will get access to irb.


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

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  endend