Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

irb

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.

like image 440
Daniel Huckstep Avatar asked Jul 17 '09 17:07

Daniel Huckstep


People also ask

How do I open IRB in terminal?

Open up IRB (which stands for Interactive Ruby). If you're using macOS open up Terminal and type irb, then hit enter. If you're using Linux, open up a shell and type irb and hit enter.

How do I load a Ruby file in IRB?

From the main menu, choose Tools | Load file/selection into IRB/Rails console.

How do I run a Ruby script in debug mode?

In order to start the Ruby debugger, load the debug library using the command-line option -r debug. The debugger stops before the first line of executable code and asks for the input of user commands.


4 Answers

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

like image 181
horseyguy Avatar answered Oct 15 '22 01:10

horseyguy


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.

like image 31
Subba Rao Avatar answered Oct 15 '22 01:10

Subba Rao


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
like image 30
rogerdpack Avatar answered Oct 15 '22 01:10

rogerdpack


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

like image 10
Tachyons Avatar answered Oct 14 '22 23:10

Tachyons