I copied the following Ruby code from the Internet and made a few changes.
#insert code here
But it doesn't work!
Please help. What can I do to debug the program by myself?
I copied the following Ruby code from the Internet and made a few changes.
#insert code here
But it doesn't work!
Please help. What can I do to debug the program by myself?
I strongly recommend this video, in order to pick the proper tool at the moment to debug our code.
https://www.youtube.com/watch?v=GwgF8GcynV0
Personally, I'd highlight two big topics in this video.
That's my two cents!
I just discovered this gem ( turns Pry into a debugger for MRI Ruby 2.0+ )
https://github.com/deivid-rodriguez/pry-byebug
To easily debug Ruby shell script, just change its first line from:
to:
Then every time when debugger console is shown, you can choose:
c
for Continue (to the next Exception, breakpoint or line with:debugger
),n
for Next line,w
/where
to Display frame/call stack,l
to Show the current code,cat
to show catchpoints.h
for more Help.See also: Debugging with ruby-debug, Key shortcuts for ruby-debug gem.
In case the script just hangs and you need a backtrace, try using
lldb
/gdb
like:and then check your process foreground.
Replace
lldb
withgdb
if works better. Prefix withsudo
to debug non-owned process.As of Ruby 2.4.0, it is easier to start an IRB REPL session in the middle of any Ruby program. Put these lines at the point in the program that you want to debug:
You can run Ruby code and print out local variables. Type Ctrl+D or
quit
to end the REPL and let Ruby program keep running.You can also use
puts
andp
to print out values from your program as it is running.deletes all the things
Welcome to 2017 ^_^
Okay, so if you're not opposed to trying out a new IDE you can do the following for free.
Quick Instructions
launch.json
to use"cwd"
and and"program"
fields using the{workspaceRoot}
macro"showDebuggerOutput"
and set it totrue
"debug.allowBreakpointsEverywhere": true
Detailed Instructions
vscode
; this is not the same as Visual Studio. It's free, light-weight, and generally positively regarded.View->Extensions
.vscode
and in there we'll but a file calledlaunch.json
where we're going to store some config options.launch.json
contents{ "version": "0.2.0", "configurations": [ { "name": "Debug Local File", "type":"Ruby", "request": "launch", "cwd": "${workspaceRoot}", "program": "{workspaceRoot}/../script_name.rb", "args": [], "showDebuggerOutput": true } ] }
File->Preferences->Settings
(or Ctrl, ) and scroll until you reach theDebug
section. Expand it and look for a field called"debug.allowBreakpointsEverywhere"
-- select that field and click on the little pencil-looking icon and set it totrue
.After doing all that fun stuff, you should be able to set breakpoints and debug in a menu similar to this for mid-2017 and a darker theme: with all the fun stuff like your call stack, variable viewer, etc.
The biggest PITA is 1) installing the pre-reqs and 2) Remembering to configure the
.vscode\launch.json
file. Only #2 should add any baggage to future projects, and you can just copy a generic enough config like the one listed above. There's probably a more general config location, but I don't know off the top of my head.All other answers already give almost everything... Just a little addition.
If you want some more IDE-like debugger (non-CLI) and are not afraid of using Vim as editor, I suggest Vim Ruby Debugger plugin for it.
Its documentation is pretty straightforward, 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.