How can I debug require in Ruby 1.9

前端 未结 1 1987
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 01:26

According to the Tin Man\'s opinion, i open a new question.

Original questions is here: How does Rubygem require all gems?

Original code i used to d

相关标签:
1条回答
  • 2021-01-22 02:11

    When rubygems is required it replaces Ruby’s Kernel.require method with its own that searches for required files in the installed gems. The integration with Ruby 1.9 and above is basically a call to require 'rubygems' during start up. This can be disabled with the (poorly documented) --disable-gems option to the ruby executable. You could make use of this to set up your debugging before explicitly calling require 'rubygems'.

    # start with ruby --disable-gems
    require 'debug' #standard library debug - doesn't load rubygems
    
    require 'rubygems' #now you can debug this
    

    If you want to use the debugger gem for your debugging it’s still possible but a little trickier as you have to load debugger without loading Rubygems. In order to do this you’ll need to manually set up your load path to include Debugger’s lib dir, as well as the lib dirs of any gems Debugger depends on. This is basically what Rubygems does for you when you call require 'debugger' with Rubygems loaded.

    To determine what libs Debugger needs, you can use this command:

     ruby -e "lp = $:.dup; gem 'debugger'; puts $: - lp"
    

    This is little Ruby script that first takes a copy of the load path ($: is the load path, you can also use $LOAD_PATH), then activates the Debugger gem, then prints out the difference between the new load path and the original. This will give you the dirs that activating debugger adds to the load path.

    On my machine this looks like this:

    $ ruby -e "lp = $:.dup; gem 'debugger'; puts $: - lp"
    /Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/columnize-0.3.6/lib
    /Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-ruby_core_source-1.2.0/lib
    /Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-linecache-1.2.0/lib
    /Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-1.5.0/lib
    

    You can now use this to create a script to use Debugger to debug require 'rubygems':

    # start with ruby --disable-gems
    
    # set up the load path without loading rubygems
    $:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/columnize-0.3.6/lib'
    $:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-ruby_core_source-1.2.0/lib'
    $:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-linecache-1.2.0/lib'
    $:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-1.5.0/lib'
    
    # require debugger and start it
    require 'debugger'
    debugger
    
    require "rubygems" #now you can debug this with debugger
    
    0 讨论(0)
提交回复
热议问题