Debugging Ruby in VS Code
Dāvis Namsons Sep 25
binding.pry is adequate for simple debugging purposes, I find that for cases when I have to quickly see multiple variable and object values, it becomes time consuming and messy. Thus I
use vscode's built-in debugging features with the ruby debug ide interface. Here's how to set it up:
Install the required gems
For JRuby or Ruby v1.8.x
gem install ruby-debug-ide gem install ruby-debug-base
For Ruby v1.9.x
gem install ruby-debug-ide gem install ruby-debug-base19x
For Ruby v2.x
gem install ruby-debug-ide gem install debase
If you don’t have it yet, install the Ruby extension and reload the editor.
In vscode, open the debugging tab and click on the dropdown, then choose the
Add configuration... option.
Ruby from the new dropdown menu. This will create a
.vscode folder with a
launch.json file in your current directory.
Launch a debugging session
Now you can run the
rdebug interface in your terminal:
rdebug-ide --host 0.0.0.0 --port 1234 --dispatcher-port 26162 /path/to/the/file.rb
(In case you need to change the port, remember to specify it in
launch.json as well)
If you're trying to debug a Rails app, you should instead specify the path of the rails script in your project with the
server command. The script is usually located in the
bin directory of your project, in which case you would write it as
After the interface is running in the terminal, you can start the debugging session. In the debug tab choose
Listen for rdebug-ide from the dropdown menu. Click the start button.
Your debugging session should be running. You can now hit your set breakpoints and use the debugging console to help analyse your Ruby code.
Here's an example: