Debugging Tools for Ruby & Rails
Whether you are debugging a flaky test, a production regression, or a background job, Ruby offers a rich toolbox—byebug, pry, the Ruby 3+ debug gem, and IDE integrations. Learn when and how to use each.
Drop into breakpoints
byebug
`bundle exec byebug` injects a REPL into any Ruby process (including tests). Use commands like `next`, `step`, `break`, and `display` to inspect state.
pry
Drop `binding.pry` into code to get syntax-highlighted introspection, command history, and plugins like `pry-rails`, `pry-byebug`.
debug gem
Ruby 3.1+ ships with `debug` (`require 'debug'`). Adds record/replay, breakpoints via `rdbg`, and remote debugging sessions.
Rails console helpers
- `rails console --sandbox` lets you explore behaviour without persisting data.
- Enable logging with `ActiveRecord::Base.logger = Logger.new(STDOUT)` while you inspect queries.
- Use `app.get '/ruby'` inside the console to replicate controller requests quickly.
Debugging background jobs & containers
Remote sessions require open ports; ensure you tunnel securely (e.g., `ssh -L 12345:localhost:12345`). Always remove debug statements before committing.
Visual debugging
- VS Code: install the Ruby extension, configure `launch.json` with `{ "command": "bundle exec rdbg --open -- bin/rails s" }` for server debugging.
- RubyMine: run any spec or server with the debugger to inspect frames, locals, and evaluate expressions inline.
- Use conditional breakpoints (`break if user.admin?`) to stop only when specific states occur.