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

后端 未结 6 615
再見小時候
再見小時候 2020-12-23 14:19

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 sta

相关标签:
6条回答
  • 2020-12-23 14:22

    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

    0 讨论(0)
  • 2020-12-23 14:30

    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
    
    0 讨论(0)
  • 2020-12-23 14:31

    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

    0 讨论(0)
  • 2020-12-23 14:32

    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.

    0 讨论(0)
  • 2020-12-23 14:45

    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

    0 讨论(0)
  • 2020-12-23 14:46

    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.

    0 讨论(0)
提交回复
热议问题