Stan Lo
Posted on July 9, 2022
Do you know Ruby's official debugger ruby/debug
provides out-of-box integration with VSCode? If you haven't tried it yet or having difficulty making it work, I hope this short post will help you set it up.
Basic Setup
- Install the VSCode rdbg extension in VSCode
- Create the
launch.json
file- Click
Run and Debug
button on the left side - Click
create a launch.json file
- this is quite small and underTo customize Run and Debug
- Save the created
launch.json
- Click
- Put
gem "debug", require: false
in yourGemfile
and runbundle install
Debug Simple Ruby Script
If you want to debug a simple Ruby script, you can follow these steps.
Debug Rails/Web Applications
If you want to debug a Rails/web application, do these instead:
- Open the files in VSCode and add some breakpoints.
- Start your program with the
rdbg
executable -bundle exec rdbg --open -n -c -- bundle exec rails s
-
--open
or-O
means starting the debugger in server mode -
-n
means don't stop at the beginning of the program, which is usually somewhere at rubygems, not helpful -
-c
means you'll be running a Ruby-based command
-
- Go back to VSCode's
Run and Debug
panel, you should see a grean play button - Click the dropdown besides the button and select
Attach with rdbg
- Click the play button
- It should now connect the VSCode to the debugger
- If it stops at somewhere in your web server (like puma). Hit
continue
orF5
. This will be resolved in the next1.6.0
release.
- If it stops at somewhere in your web server (like puma). Hit
- Send some requests and it should stop at your breakpoints
Video
I also have built this repl.it
to let you try out ruby/debug
's console commands in your browser (it'll only take you 5 minutes).
If you want to see more articles/tips about the powerful features ruby/debug
has, you can follow me on Twitter 🙂
💖 💪 🙅 🚩
Stan Lo
Posted on July 9, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.