在Ruby中调试第三方宝石的最佳方式 [英] Best way to debug third-party gems in ruby
问题描述
由于ruby宝石中可能有很多 Ghost方法,所以我不认为通过静态读取它的源代码来研究ruby gem的内部机制是不错的。有没有办法将第三部分宝石的源文件附加到正在运行的ruby进程进行调试,以便我可以设置断点并查看事情的动态变化?
BTW,我试过通过点击require语句或第三部分gem的其他符号( require')的上下文菜单Go To-> Implementations,导航到RubyMine中第三部分gem的源文件watir'
),没有成功。对于Ruby等动态打字语言的IDE来说,这是正常的吗?
Since there may be a lot of Ghost Methods inside a ruby gem, I don't think it is a good idea to study the inner mechanism of a ruby gem just by reading its source code statically. Is there a way to attach the source file of a third-part gem to a running ruby process for debugging so that I can set break point and see how things work dynamically ?
BTW,I've tried to navigate to the source file of a third-part gem in RubyMine by clicking on the context menu "Go To->Implementations" of the 'require' statement or other symbol of an third-part gem( require 'watir'
for example ), without success. Is it normal for an IDE of a dynamic typing language such as Ruby to fail a symbol navigation?
推荐答案
我很想知道如果有一个更好的方式来做,但是我通常做的是:
I would love to know if there's a better way to do this, but how I usually do it is:
- 将ruby-debug gem添加到Gemfile(或ruby-debug19如果你使用的是Ruby 1.9.2)
- 通过执行
bundle show gemname
找到宝石。我在Mac上,所以我通常把它管理到pbcopy,所以它被复制到我的剪贴板。bundle show rails | pbcopy
- 在您最喜欢的编辑器中打开gem目录。
mvim / path / to / gem / directory
- 浏览到要放置断点*的文件和行,并插入
调试器
- 重新加载页面,运行测试或做任何您将获得Gem文件执行
- 当调试器执行停止时,您可以检查变量(
p variable_name
),并逐行移动 ruby debugger命令。
- Add the ruby-debug gem to your Gemfile (or ruby-debug19 if you're on Ruby 1.9.2)
- Find the Gem by doing
bundle show gemname
. I'm on a Mac so I usually pipe this to pbcopy so it gets copied to my clipboard.bundle show rails | pbcopy
- Open the gem directory in your favorite editor.
mvim /path/to/gem/directory
- Navigate to the file and line where you want to put the breakpoint* and insert
debugger
above the line in question. - Reload page, run test, or do whatever you would to get the Gem file to execute
- When execution stops at debugger, you can inspect variables (
p variable_name
), and move line by line with the ruby debugger commands.
*知道放置断点可以对代码进行一些了解,但是您应该从lib / gemname.rb开始
*Knowing where to put the breakpoint can take some understanding of the code, but you should start in lib/gemname.rb
这篇关于在Ruby中调试第三方宝石的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!