How to debug ruby code?
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.
How to debug Ruby scripts
Use Pry (GitHub).
Install via:
$ gem install pry
$ pry
Then add:
require 'pry'; binding.pry
into your program.
As of pry
0.12.2 however, there are no navigation commands such as next
, break
, etc. Some other gems additionally provide this, see for example pry-byebug
.
How to debug Ruby code on Visual Studio Code?
This is actually fairly easy once you follow the right steps.
First, you have to download Ruby Extension which is available on vs code marketplace or simply via Extension tab in VS code itself: just search for Ruby, install it, and reload VS Code [update: vscode can perfectly load new extensions without needing a reload].
secondly, you have to follow the debugging guide for this extension which is available on the GitHub link I provided or in vs code marketplace. Here's the section you would be most interested in, but you could also see the original wiki:
Install Ruby Dependencies
In this extension, we implement ruby debug ide protocol to allow VS
Code to communicate with ruby debug, it requires ruby-debug-ide to be
installed on your machine. This is also how RubyMine/NetBeans does by
default.
If you are using JRuby or Ruby v1.8.x (jruby, ruby_18, mingw_18), run
gem install ruby-debug-ide
The latest version is 0.6.0. Make sure
ruby-debug-base
is installed together withruby-debug-ide
.
If you are using Ruby v1.9.x (ruby_19, mingw_19), run
gem install ruby-debug-ide
The latest version is 0.6.0. Make sure
ruby-debug-base19x
is installed together withruby-debug-ide
.
If you are using Ruby v2.x
gem install ruby-debug-ide -v 0.6.0 (or higher versions)
gem install debase -v 0.2.1 (or higher versions)Add VS Code config to your project
Go to the debugger view of VS Code and hit the gear icon. Choose Ruby
or Ruby Debugger from the prompt window, then you'll get the sample
launch config in .vscode/launch.json. The sample launch configurations
include debuggers for RSpec (complete, and active spec file) and
Cucumber runs. These examples expect that bundle install --binstubs
has been called. Detailed instruction for debugging Ruby
Scripts/Rails/etcRead following instructions about how to debug ruby/rails/etc locally
or remotely01 Debugger installation
02 Launching from VS Code
03 Attaching to a debugger
04 Running gem scripts
05 Example configurations
if you follow these steps, you will have every dependency installed in Step 1.
Step 2 helps you config your project workspace to start debuging codes written in ruby.
by finishing step 2, you should be able to start debugging.
here is a simple config that I use in my recent ruby project to simply debug the current open file. this is fully explained in the second step I linked
{
"name": "Debug Local File",
"type": "Ruby",
"request": "launch",
"cwd": "${workspaceRoot}",
"program": "${file}"
}
"program": "${file}" is the line that enables debugging the current open file.
How to debug Ruby code under Docker with VSCode?
In github repo of this extension there is an issue about that. I suggest you to check it out. Basically here is a suggestion from there:
put it to Dockerfile in CMD:
rdebug-ide --host 0.0.0.0 --port 1234 -- bin/rails s -b 0.0.0.0
Debugging (Old) Ruby Rack via VsCode
I ran a debug session with your project using the Ruby extension.
As explained in the vscode-ruby debugger docs, first I had to install the ruby-debug-ide
and the debase
gems.
Then I used the following launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Rack",
"type": "Ruby",
"request": "launch",
"program": "~/.rbenv/versions/2.4.10/bin/rackup",
"args": []
}
]
}
If there's any issue starting the session, you should be able to see error logs in the debug console panel in VSCode.
Related Topics
What Command Opens Ruby's Repl
How to Use the Rails Helper "Distance_Of_Time_In_Words" in Plain Old Ruby (Non-Rails)
How to Compare a String Against Multiple Other Strings
Why Doesn't Rails' "Errors.Full_Messages" Replace Attribute and Message Variables
Rails: Plus Sign in Get-Request Replaced by Space
Why Is 'Return a or B' a Void Value Expression Error in Ruby
Could Not Find 'Cocoapods' (>= 0) Among N Total Gem(S) (Gem::Loaderror)
Error When Doing Rake Db:Migrate on Heroku
Retrieving a Braintree Customer's Subscriptions
How to Determine the Md5 Digest of a Given Asset in the Rails Asset Pipeline
Devise: How to Override Devise Error Messages on Password Change
Rendering a Partial from a Controller in Rails
Factorygirl Association Model Trouble: "Systemstackerror: Stack Level Too Deep"
Why Is Ruby Unable to Verify an Ssl Certificate
Changing Field Separator/Delimiter in Exported CSV Using Ruby CSV