如何调试ruby代码? [英] How to debug ruby code?

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

问题描述

我运行Ubuntu 10.10。我只想调试一个简单的脚本。花了半天时间试图找出如何做到这一点我放弃了。我应该做什么?



我从Ubuntu存储库安装了 ruby​​-dev

我运行 sudo gem install ruby​​-debug sudo gem install ruby​​-debug-ide



我尝试了一些不同的方法来使这项工作。我尝试 require'ruby-debug',然后在代码中的某个地方设置 debugger 。但是Ruby不会找到 ruby​​-debug

我尝试设置 vim-ruby-debugger ,这将采取年龄执行:Rdebugger myScript.rb ,并允许我设置断点,但似乎没有一种使用该调试器执行我的代码的方法。

我尝试使用NetBeans,每次我设置项目并点击任何东西,它就会崩溃。



所以,亲爱的社区:必须有一种方法来调试Ruby。不是Rails。没什么好想的只是一些CLI脚本。请帮助我,或者我失去了我的理智。



编辑:
gem exec dir不在我的路径。所以,至少rdebug似乎现在可以工作。

解决方案

Ruby-debug是1.8+,ruby-debug19是1.9+ 。



ruby​​-debug很容易学习,非常实用。您可以告诉应用程序运行,直到某个条件存在,然后将其中断,从而轻松找到零值或偶尔发生的其他条件。



从命令行使用 rdebug appname ,最后在调试器提示符下。如果要运行到第100行并停止,您可以输入 c 100 ,调试器将设置一个临时断点,程序将运行,然后停在那里,如果它在执行路径。一旦停止,临时断点将被清除。如果您一直想要停止在第100行,您可以执行 b 100 然后 c ,并且调试器将设置永久断点点,继续,然后在达到断点时停止。您可以清除断点,设置在某些条件适用时发生的条件等。您可以键入 n ,以跳过下一个跳过子程序调用的指令,或 s 进入他们。有以各种方式显示变量内容的命令,请阅读文档。



从里面的rdebug你可以放入一个IRB shell,你的变量已经被填充,所以你可以捅事情看看会发生什么。从里面也可以检查或设定价值,帮助进行假设调整。如果你在rdebug里面做到这一点,你可以用改变的值来继续这个程序,看看它的行为如何。



IRB有它的位置,这是伟大的尝试事情,但它不是替代调试器,就像调试器可以做一些IRB-ish的事情,但不会取代它。这两种工具都是一个很好的组合,并且打败了依靠打印语句或倾销到日志文件。






< a href =https://github.com/pry/pry =nofollow noreferrer> Pry 已经成为IRB和调试器,非常值得调查。


I run Ubuntu 10.10. I just want to debug a simple script. After spending half a day trying to figure out how that could be done I give up. What the heck am I supposed to do?

I installed ruby-dev from the Ubuntu repository
I ran sudo gem install ruby-debug and sudo gem install ruby-debug-ide

I tried a few different ways to make this work. I tried require 'ruby-debug' and then setting debugger somewhere in the code. But Ruby won't find ruby-debug.
I tried setting up vim-ruby-debugger, which will take ages to execute :Rdebugger myScript.rb and will allow me to set breakpoints, but there doesn't seem to be a way to execute my code using that debugger.
And I tried to use NetBeans which simply crashed every time I set up the project and clicked anything.

So, dear community: There must be a way to debug Ruby. Not Rails. Nothing fancy. Just some CLI script. Please help me or I lose what is left of my sanity.

Edit: the gem exec dir was not in my path. So, at least rdebug seems to work now.

解决方案

Ruby-debug is for 1.8+ and ruby-debug19 is for 1.9+.

ruby-debug is easy to learn and very useful. You can tell the application to run until a certain condition exists, then have it break, making it easy to locate nil values, or other conditions that occur sporadically.

From the command-line use rdebug appname and you'll end up at the debugger prompt. If you want to run to line 100 and stop you can enter c 100 and the debugger will set a temporary break-point, the program will run then stop there if it's in the execution path. Once it stops the temporary break-point will be cleared. If you always want to stop at line 100 you could do b 100 then c and the debugger will set a permanent break-point, continue, then stop when the break-point is reached. You can clear the breakpoints, set conditional ones that occur when certain conditions apply, etc. You can type n to step to the next instruction skipping over subroutine calls, or s to step into them. There are commands to display contents of variables in various ways, so read through the docs.

From inside rdebug you can drop into an IRB shell with your variables already populated so you can poke at things to see what happens. From inside either you can inspect or set values, helping with what-if adjustments. If you do that from within rdebug you can continue the program with the altered value(s) and see how it behaves.

IRB has its place, and it's great for trying things, but it's not a substitute for the debugger, just as the debugger can do some IRB-ish things, but won't replace it. Both tools are a good combination and beat the heck out of relying on print statements or dumping to a log file.


Pry has emerged as a great combination of IRB and a debugger, and is well worth investigating.

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

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