Ruby n00b here. I copied the following ruby code from the internets and made a few changes.
#insert teh codes here
But it doesn't work!
Please help. What can I do to debug the program by myself?
Thanks in advance,
Andrew Grimm
|
|
|||||
|
Use Pry: http://pry.github.com |
|||||||
|
|
|||||||
|
|
|||||
|
All other answers already give almost everyting... Just a little addition. If you want some more IDE-like debugger (not-CLI) and not afraid of using Vim as editor, I suggest Vim Ruby Debugger plugin for it. Its documentation is pretty straitforward, so follow the link and see. In short, it allows you to set breakpoint at current line in editor, view local variables in nifty window on pause, step over/into — almost all usual debugger features. For me it was pretty enjoyable to use this vim debugger for debugging a Rails app, although rich logger abilities of Rails almost eliminates the need for it. |
|||
|
As banister recommended: use pry! I can only agree on this. pry is a much better repl than irb. You need to add
to your source file and then insert a breakpoint in your source code by adding
at the place where you want to have a look at the things (this is like triggering a breakpoint in a classic IDE environment) Once your program hits the
line, you'll be thrown right into the pry repl, with all the context of your program right at hand, so that you can simply explore everything around, investigate all objects, change state, and even change code on the fly. I believe you can not change the code of the method that you are currently in, so you can sadly not change the next line to be executed. But good ruby code tends to be single line anyway ;-) |
|||
|
The mother of all debugger is plain old print screen. Most of the time, you probably only want to inspect some simple objects, a quick and easy way is like this:
This will print out the contents of @result to STDOUT with a line in front for easy identification. Bonus if you use a autoload / reload capable framework like Rails, you won't even need to restart your app. (Unless the code you are debugging is not reloaded due to framework specific settings) I find this works for 90% of the use case for me. You can also use ruby-debug, but I find it overkill most of the time. |
|||
|
printf debugging There has always been a controversy around debugging techniques, some people like to debug by print statements, some other ones like to dig deep with a debugger. I'd suggest that you try both approaches. Actually one of the old Unix men recently said, that printf debugging was a faster way to go for him at some points. But if you are new at some job and need to understand a big blob of code, then it's really usefull to step throughout there, putting some breakpoints here and there, going along with it how it works. It should give you some understanding how the code is weaved. If you are new to some other peoples software, It might help you to step through there. You'll quickly find out if they arranged it in a clever way, or if that's just a bunch of shit. |
|||
|