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?

推荐答案

我很想知道是否有更好的方法,但我通常的做法是:

  1. 将ruby调试gem添加到gem文件中(如果使用ruby 1.9.2,则添加ruby-debug19)
  2. 通过做bundle show gemname找到Ruby .我在Mac电脑上,所以我通常会把它传送到pbcopy,这样它就会被复制到我的剪贴板上.bundle show rails | pbcopy
  3. 在你最喜欢的编辑器中打开gem目录.mvim /path/to/gem/directory
  4. 导航到要放置断点*的文件和行,并在相关行上方插入debugger.
  5. 重新加载页面,运行测试,或者做任何你想让Gem文件执行的事情
  6. 当执行在调试器停止时,您可以判断变量(p variable_name),并使用ruby debugger commands逐行移动.

*知道将断点放在哪里可能需要对代码有一些了解,但应该从lib/gemname开始.rb

Ruby相关问答推荐

如何在 Ruby 中正确编写代码?以便它产生正确的输出?

Rb:仅通过在 Ruby 中的类上添加 each 方法,可枚举模块是如何工作的?

在 rake 任务中提问

在 YAML 变量中包含 jekyll / liquid 模板数据?

Rails 控制台中没有数据库连接

将方法应用于数组/可枚举中的每个元素

Ruby 中的 Object 和 BasicObject 有什么区别?

在文件中搜索字符串的最佳方法是什么?

在包含任意数量的嵌套散列和数组的散列中查找键/值对

Vagrant - 如何拥有特定于主机平台的配置步骤

在Ruby中第一个=符号之后获取子字符串

铲子 (<<) 运算符如何在 Ruby 哈希中工作?

相当于 Ruby 中的通过

带有可选参数的方法

如何使用 RVM 更新 Ruby 解释器?

在 Ruby 中获取用户主目录的跨平台方法?

obj.nil?与 obj == nil

等号 ('=') 放在方法定义中的方法名称之后有什么作用?

将哈希传递给函数 ( *args ) 及其含义

JavaScript Array:获取项目的范围