43

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Daniel Huckstep
  • 5,368
  • 10
  • 40
  • 56

6 Answers6

46

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 = 10
binding.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

horseyguy
  • 29,455
  • 20
  • 103
  • 145
29

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
  • 10,576
  • 7
  • 29
  • 31
14

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
  end
end
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • 2
    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 Jul 29 '10 at 21:10
11

This feature is available from Ruby 2.4. You can just use 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.

Source: http://blog.redpanthers.co/new-binding-irb-introduced-ruby-2-4/

Ruby commit: https://github.com/ruby/ruby/commit/493e48897421d176a8faf0f0820323d79ecdf94a

Tachyons
  • 2,131
  • 1
  • 21
  • 35
0

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

Yuming Cao
  • 935
  • 1
  • 10
  • 22
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.

spyle
  • 1,960
  • 26
  • 23