tags:

views:

511

answers:

2

Can I drop to an irb prompt from a 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.

+9  A: 

you can use ruby-debug to get access to irb

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

when program reaches debugger you will get access to irb.

Subba Rao
Will this take the binding context into account? Can I set the binding to something else?
John F. Miller
Duh. I'm an idiot. Thanks!
Daniel Huckstep
+1  A: 

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 # :nodoc:
  def self.start_session(binding)
    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
rogerdpack
Excellent find, thanks! This is exactly what I needed to access the class and instance variables in the context of where I invoked IRB.start_session(binding).
csmosx