Daniel Huckstep
Daniel Huckstep

Reputation: 5408

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

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.

Upvotes: 43

Views: 12621

Answers (6)

spyle
spyle

Reputation: 2008

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.

Upvotes: 0

Tachyons
Tachyons

Reputation: 2171

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

Upvotes: 13

rogerdpack
rogerdpack

Reputation: 66961

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

Upvotes: 14

Yuming Cao
Yuming Cao

Reputation: 955

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

Upvotes: 0

horseyguy
horseyguy

Reputation: 29915

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

Upvotes: 46

Subba Rao
Subba Rao

Reputation: 10696

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.

Upvotes: 29

Related Questions