我如何在Ruby 1.9中调试require [英] How can I debug require in Ruby 1.9

查看:70
本文介绍了我如何在Ruby 1.9中调试require的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 the Tin Man 的意见,我开了一个新问题。



原始问题在这里: Rubygem如何需要所有宝石?



原始代码i用于调试:

  require'debugger'
调试器
require'thor'

以下是两难局面:


  1. 使用默认的 -rdebug 进行调试,我不能转到Rubygem的源代码。使用调试器
  2. c $ c> gem,在调试之前我必须 require >(我在前一个问题中尝试的方式,这是不可接受的,因为在我到达之前发生了一些重要的事情)

希望能找到调试IT的方法。 / div>

当需要 ruby​​gems 用自己的方法替换了Ruby的 Kernel.require 方法,该方法搜索安装的gem中的必需文件。与Ruby 1.9及以上版本的集成基本上是调用在启动时需要'rubygems' 。对于 ruby​​ 可执行文件,这可以通过(文档不完整) - disable-gems 选项禁用。在显式调用 require'rubygems'之前,可以使用它来设置调试。

- ruby​​ --disable-gems
require'debug'#标准库debug - 不加载rubygems

require' rubygems'#现在你可以调试这个

如果你想使用 debugger gem 对于您的调试仍然可能,但有点棘手,因为您必须加载调试器不加载Rubygems。为了做到这一点,您需要手动设置加载路径以包含Debugger的lib目录,以及Debugger依赖的任何Gem的lib目录。这基本上是Rubygems为您调用 require'debugger'加载Rubygems时为您做的。



确定什么libs调试器需要,你可以使用这个命令:

  ruby​​ -elp = $ :. dup; gem'debugger'; puts $: -  lp

这是首先需要的Ruby脚本加载路径的副本( $:是加载路径,您也可以使用 $ LOAD_PATH ),然后激活调试器gem,然后打印新的加载路径和原始之间的区别。这会给你启动调试器添加到加载路径的目录。



在我的机器上,这看起来像这样:


$ b

  $ ruby​​ -elp = $ :. 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

现在可以使用它创建一个脚本来使用调试器来调试 require'rubygems'

#开始使用ruby --disable-gems

#设置加载路径而不加载rubygems
$ :. unshift'/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/columnize-0.3.6/lib'
$ :. unshift'/ Users /亚光/ .rvm /宝石/红宝石1.9.3-P385 /宝石s / 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'

#需要调试器和启动它
需要'debugger'
调试器

需要rubygems#现在你可以用调试器来调试它


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 debug:

require 'debugger'
debugger
require 'thor'

Here's the dilemma:

  1. debug with the default -rdebug, i cant go to Rubygem's source code
  2. debug with debugger gem, i have to require before i debug (the way i tried in the previous question, which is unacceptable because something important had happened before i get there)

Hope to find a way to debug IT.

解决方案

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

这篇关于我如何在Ruby 1.9中调试require的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆