Debugging with the debug gem
Yesterday we talked about using binding.irb for debugging. It's a great tool, but if you want something with the ability to step through code, debug ships as a standard gem, making it a convenient option for more advanced debugging sessions.
You can start a debug session by executing binding.break or debugger in your code after requiring the gem. Once started, running help in the debug console will show you all the available commands, such as s / step, n / next and c / continue for stepping through code.
Caveats
The debugger commands take precedence over regular Ruby code. So if you e.g. try to assign c = MyHttpClient.new in the debug console, it will be interpreted as the continue command instead.
You will also lose auto-completion and the ability to do multi-line editing in the debug console. You can work around this by running the irb command from within the debugger, which switches to the debugger's IRB mode, which is closer to a regular IRB session, but still supports all the debug commands.
History
The debug gem first shipped in Ruby version 3.1, released on Christmas 2021.
Prior to having a standard gem for debugging, it was common to use open source gems like pry and byebug. Some developers probably swear by these gems to this day, but I personally prefer to avoid extra dependencies whenever possible.