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

6

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.

Thrasonical answered 17/7, 2009 at 17:7 Comment(0)
A
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.

Adest answered 17/7, 2009 at 17:23 Comment(3)
Will this take the binding context into account? Can I set the binding to something else?Animalism
This requires the ruby-debug gem, which doesn't work with Ruby 1.9.Motorcade
for 1.9 you can use ruby-debug19 or the debugger gem https://mcmap.net/q/144819/-ruby-debug-with-ruby-1-9-3Poitiers
P
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

Professionalism answered 18/8, 2011 at 5:30 Comment(1)
Thanks a lot, this should the at the top, I was on my way to use IRB for thatRenitarenitent
A
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.

Adest answered 17/7, 2009 at 17:23 Comment(3)
Will this take the binding context into account? Can I set the binding to something else?Animalism
This requires the ruby-debug gem, which doesn't work with Ruby 1.9.Motorcade
for 1.9 you can use ruby-debug19 or the debugger gem https://mcmap.net/q/144819/-ruby-debug-with-ruby-1-9-3Poitiers
P
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
Poitiers answered 27/1, 2010 at 4:57 Comment(1)
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).Nicholas
C
13

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

Cameral answered 11/12, 2016 at 1:42 Comment(0)
M
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

Michalemichalski answered 1/11, 2014 at 19:24 Comment(0)
L
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.

Lavish answered 6/2, 2018 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.