Debugging with the debug gem
suggest changeThe debug gem is Ruby's official debugger. It has shipped with Ruby as a bundled gem since Ruby 3.1, replacing the old lib/debug.rb, and it is what rdbg, the VS Code Ruby debugger and Rails' default Gemfile use. Compared to Pry and Byebug it needs no extra installation, keeps working on new Ruby versions, and adds catchpoints, watchpoints, tracers and remote debugging.
This article covers stopping a program (binding.break, debugger, rdbg), stepping, inspecting variables and frames, line, method, conditional and exception breakpoints, watchpoints, tracers, scripting the debugger, attaching to a running process, VS Code, and the IRB integration. All examples were run with Ruby 4.0 and debug 1.11.1. Everything shown also works on Ruby 3.1 and newer, except Data.define in the first example (Ruby 3.2).
Installing and checking the version
On Ruby 3.1+ the gem is already installed:
$ gem list debug
debug (1.11.1)
$ rdbg --version
rdbg 1.11.1
debug is a bundled gem, not a default gem. Bundler only allows gems that are in the Gemfile, so in a Bundler project you have to add it there:
# Gemfile
group :development, :test do
gem "debug", require: "debug/prelude"
end
require: "debug/prelude" defines debugger and binding.break without loading the debugger itself. The real debugger loads the first time a breakpoint is hit, so there is no cost at boot. This is what Rails 7.2+ puts in new applications. On Ruby 2.7 and 3.0 you can gem install debug to get the same gem.
Two ways to start
There are two ways to get into the debugger:
- Put a breakpoint in the code with
binding.break(aliasesbinding.banddebugger) afterrequire "debug", and run the program normally withruby. - Run the program under the debugger with
rdbg script.rb(instead ofruby script.rb) and set breakpoints from the debugger console.
A breakpoint in the code
require "debug"
def parse_qty(str)
Integer(str)
end
def import(rows)
rows.filter_map do |sku, qty|
debugger if sku == "C3"
[sku, parse_qty(qty)]
end
end
rows = [["A1", "2"], ["B2", "10"], ["C3", "1"], ["D4", "4"]]
p import(rows)
Running ruby orders.rb stops at the debugger call, shows the code around it and the top frames, and waits for commands at the (rdbg) prompt. In the transcripts in this article, the commands typed at the prompt are shown after (rdbg):
[7, 10] in orders.rb
7| def import(rows)
8| rows.filter_map do |sku, qty|
=> 9| debugger if sku == "C3"
10| [sku, parse_qty(qty)]
=>#0 block {|sku="C3", qty="1"|} in Object#import at orders.rb:9
#1 [C] Array#each at #<none>:0
# and 3 frames (use `bt' command for all frames)
(rdbg) info locals
%self = main
sku = "C3"
qty = "1"
rows = [["A1", "2"], ["B2", "10"], ["C3", "1"], ["D4", "4"]]
(rdbg) p sku
=> "C3"
(rdbg) c
[["A1", 2], ["B2", 10], ["C3", 1], ["D4", 4]]
The frame line shows the block's parameters (sku="C3", qty="1"), and info locals also lists rows, the variable from the enclosing method that the block can see. c (short for continue) resumes the program.
Because debugger is a normal method call, if makes it conditional. This is often the quickest way to stop on the one iteration you care about.
Note: by default the debugger shows 10 lines of source at each stop. To keep the transcripts short, all examples in this article set RUBY_DEBUG_SHOW_SRC_LINES=4. All settings are covered in Configuration.
Running under rdbg
rdbg runs a script with the debugger loaded and stops before the first line, so the script doesn't need require "debug". Take this script:
class Cart
Item = Data.define(:name, :price, :qty)
def initialize
@items = []
end
def add(name, price, qty = 1)
@items << Item.new(name:, price:, qty:)
self
end
def subtotal
@items.sum { |item| item.price * item.qty }
end
def total(discount_pct)
discount = subtotal * discount_pct / 100
subtotal - discount
end
end
cart = Cart.new
cart.add("book", 30).add("pen", 2, 5)
puts "Total: #{cart.total(10)}"
Start it with rdbg cart.rb, set a breakpoint on line 18 and continue:
[1, 4] in cart.rb
=> 1| class Cart
2| Item = Data.define(:name, :price, :qty)
3|
4| def initialize
=>#0 <main> at cart.rb:1
(rdbg) break 18
#0 BP - Line /path/to/cart.rb:18 (call)
(rdbg) continue
[16, 19] in cart.rb
16|
17| def total(discount_pct)
=> 18| discount = subtotal * discount_pct / 100
19| subtotal - discount
=>#0 Cart#total(discount_pct=10) at cart.rb:18
#1 <main> at cart.rb:25
Stop by #0 BP - Line /path/to/cart.rb:18 (call)
Other ways to start rdbg:
rdbg cart.rb arg1 arg2 # like: ruby cart.rb arg1 arg2
rdbg -n cart.rb # don't stop at the first line, only at breakpoints
rdbg -- -Ilib -rjson cart.rb # pass options to ruby itself
rdbg -c -- bundle exec rspec spec/cart_spec.rb # debug a command from $PATH
rdbg -c -- bin/rails server
The -c (command) mode is how you debug programs started by a launcher such as rake, rspec or rails.
Stepping through code
Continuing the session above, which is stopped at line 18:
(rdbg) step
[12, 15] in cart.rb
12|
13| def subtotal
=> 14| @items.sum { |item| item.price * item.qty }
15| end
=>#0 Cart#subtotal at cart.rb:14
#1 Cart#total(discount_pct=10) at cart.rb:18
# and 1 frames (use `bt' command for all frames)
(rdbg) finish
[13, 16] in cart.rb
13| def subtotal
14| @items.sum { |item| item.price * item.qty }
=> 15| end
16|
=>#0 Cart#subtotal at cart.rb:15 #=> 40
#1 Cart#total(discount_pct=10) at cart.rb:18
# and 1 frames (use `bt' command for all frames)
(rdbg) next
[17, 20] in cart.rb
17| def total(discount_pct)
18| discount = subtotal * discount_pct / 100
=> 19| subtotal - discount
20| end
=>#0 Cart#total(discount_pct=10) at cart.rb:19
#1 <main> at cart.rb:25
(rdbg) next
[18, 21] in cart.rb
18| discount = subtotal * discount_pct / 100
19| subtotal - discount
=> 20| end
21| end
=>#0 Cart#total(discount_pct=10) at cart.rb:20 #=> 36
#1 <main> at cart.rb:25
(rdbg) p discount
=> 4
(rdbg) up
=> 25| puts "Total: #{cart.total(10)}"
=>#1 <main> at cart.rb:25
(rdbg) info locals
%self = main
cart = #<Cart:0x0000013c4b67d850 @items=[#<data Cart::Item name="book", price=30, qty=1>, #<data Cart::Item name="pe...>
(rdbg) continue
Total: 36
step(s) runs to the next line, entering method calls, so it stopped insidesubtotal.finish(fin) runs until the current method returns. It stops at the method'sendand shows the return value:#=> 40.next(n) runs to the next line in the current method, stepping over calls.continue(c) runs until the next breakpoint or the end of the program.
When a method is about to return, the frame line shows the return value (#=> 36 above), so you don't need to add temporary variables to see what a method returns. step and next take a count (next 3). until (u) runs until a line after the current one is reached, which is the quick way out of a loop.
Pressing Enter on an empty line repeats the last command, so you can type n once and then keep pressing Enter.
Inspecting state
At the prompt you can type any Ruby expression and it is evaluated in the current frame:
| Command | What it shows |
|---|---|
p expr, pp expr |
the value of expr (pretty-printed with pp) |
| any Ruby expression | evaluated in the current frame and its result shown |
info (i) |
locals and instance variables of the current frame |
info locals, info ivars, info consts, info globals |
one kind of variable |
ls (also outline, o) |
methods, instance variables and locals visible from self |
bt (backtrace), bt 10, bt /regexp/ |
the call stack, optionally limited or filtered |
frame N (f), up, down |
move to another frame in the stack |
list (l), whereami |
more source around the current line |
display expr |
show expr after every stop (undisplay N removes it) |
info breakpoints |
breakpoints, catchpoints and watchpoints |
up, down and frame change the frame that expressions are evaluated in. In the session above, up moved to <main>, where info locals shows cart instead of the method's locals. Moving between frames does not change where the program will continue from.
If a variable has the same name as a debugger command (n, s, c, p, list), typing the name alone runs the command. Use p n or eval n to see the variable.
ls and display on an Account object stopped in withdraw:
(rdbg) ls
Account#methods: balance deposit withdraw
instance variables: @balance
locals: amount
(rdbg) display @balance
0: @balance = 165
(rdbg) next
...
=>#0 Account#withdraw(amount=30) at account.rb:14 #=> 135
#1 <main> at account.rb:19
0: @balance = 135
Breakpoints
break (b) accepts several forms:
break 18 # line 18 of the current file
break cart.rb:18 # line in a file
break Cart#total # instance method
break Cart.create # class/singleton method
break cart.rb:18 if: qty > 3 # conditional
break Cart#total do: info # run a command, then continue
break Cart#total pre: info # run a command, then stop
break if: @items.empty? # stop on any line where the condition is true
A method breakpoint can be set before the class exists. rdbg stops before the first line runs, so in the next session Account isn't defined yet and the debugger reports it, keeps the breakpoint as pending, and activates it once the method is defined. This is the program:
class Account
attr_reader :balance
def initialize(balance)
@balance = balance
end
def deposit(amount)
@balance += amount
end
def withdraw(amount)
@balance -= amount
end
end
acct = Account.new(100)
[20, -5, 50].each { |amt| acct.deposit(amt) }
acct.withdraw(30)
puts acct.balance
Stopping only on negative deposits, then watching the balance:
(rdbg) break Account#deposit if: amount < 0
Unknown constant name: "Account"
#0 BP - Method (pending) Account#deposit if: amount < 0
(rdbg) c
[7, 10] in account.rb
7|
8| def deposit(amount)
=> 9| @balance += amount
10| end
=>#0 Account#deposit(amount=-5) at account.rb:9
#1 block {|amt=-5|} in <main> at account.rb:18
# and 2 frames (use `bt' command for all frames)
Stop by #0 BP - Method Account#deposit at account.rb:8 if: amount < 0
(rdbg) info
%self = #<Account:0x000001134000b0c8 @balance=120>
amount = -5
@balance = 120
(rdbg) watch @balance
#1 BP - Watch #<Account:0x000001134000b0c8> @balance = 120
(rdbg) c
[8, 11] in account.rb
8| def deposit(amount)
9| @balance += amount
=> 10| end
11|
=>#0 Account#deposit(amount=-5) at account.rb:10 #=> 115
#1 block {|amt=-5|} in <main> at account.rb:18
# and 2 frames (use `bt' command for all frames)
Stop by #1 BP - Watch #<Account:0x000001134000b0c8> @balance = 120 -> 115
(rdbg) c
[8, 11] in account.rb
8| def deposit(amount)
9| @balance += amount
=> 10| end
11|
=>#0 Account#deposit(amount=50) at account.rb:10 #=> 165
#1 block {|amt=50|} in <main> at account.rb:18
# and 2 frames (use `bt' command for all frames)
Stop by #1 BP - Watch #<Account:0x000001134000b0c8> @balance = 115 -> 165
(rdbg) info breakpoints
#0 BP - Method Account#deposit at account.rb:8 if: amount < 0
#1 BP - Watch #<Account:0x000001134000b0c8> @balance = 115 -> 165
(rdbg) delete 1
deleted: #1 BP - Watch #<Account:0x000001134000b0c8> @balance = 115 -> 165
(rdbg) c
135
watch @ivar stops whenever that instance variable of the current self changes, and shows the old and new value. It watches one object, not every Account. delete N removes one breakpoint and delete with no argument removes all of them (after asking for confirmation).
Breakpoint options in the code
binding.break takes the same do: and pre: options. With do: the debugger runs the command and continues without waiting for input, which turns a breakpoint into a temporary log statement:
require "debug"
def average(values)
total = values.sum
binding.break(do: "info locals")
total / values.size
end
p average([3, 4, 5, 6])
p average([1.5, 2.5])
[3, 6] in report.rb
3| def average(values)
4| total = values.sum
=> 5| binding.break(do: "info locals")
6| total / values.size
=>#0 Object#average(values=[3, 4, 5, 6]) at report.rb:5
#1 <main> at report.rb:9
(rdbg:#debugger) info locals
%self = main
values = [3, 4, 5, 6]
total = 18
4
[3, 6] in report.rb
3| def average(values)
4| total = values.sum
=> 5| binding.break(do: "info locals")
6| total / values.size
=>#0 Object#average(values=[1.5, 2.5]) at report.rb:5
#1 <main> at report.rb:10
(rdbg:#debugger) info locals
%self = main
values = [1.5, 2.5]
total = 4.0
2.0
The output shows the bug: with integers total is 18, so 18 / 4 is integer division and the average is 4 instead of 4.5.
Catchpoints: stopping when an exception is raised
catch ExceptionClass stops at the point where an exception of that class (or a subclass) is raised, before any rescue runs and before the stack unwinds. That way you see the state that caused the error, not just the stack trace:
def parse_qty(str)
Integer(str, exception: false) or raise ArgumentError, "bad quantity: #{str.inspect}"
end
def import(rows)
rows.filter_map do |sku, qty|
[sku, parse_qty(qty)]
end
end
rows = [["A1", "2"], ["B2", "10"], ["C3", "1"], ["D4", "x"]]
p import(rows)
(rdbg) catch ArgumentError
#0 BP - Catch "ArgumentError"
(rdbg) c
[1, 4] in import.rb
1| def parse_qty(str)
=> 2| Integer(str, exception: false) or raise ArgumentError, "bad quantity: #{str.inspect}"
3| end
4|
=>#0 Object#parse_qty(str="x") at import.rb:2
#1 block {|sku="D4", qty="x"|} in Object#import at import.rb:7
# and 4 frames (use `bt' command for all frames)
Stop by #0 BP - Catch "ArgumentError"
(rdbg) bt
=>#0 Object#parse_qty(str="x") at import.rb:2
#1 block {|sku="D4", qty="x"|} in Object#import at import.rb:7
#2 [C] Array#each at #<none>:0
#3 [C] Enumerable#filter_map at #<none>:0
#4 Object#import(rows=[["A1", "2"], ["B2", "10"], ["C3", "1"],...) at import.rb:6
#5 <main> at import.rb:12
(rdbg) frame 1
=> 7| [sku, parse_qty(qty)]
=>#1 block {|sku="D4", qty="x"|} in Object#import at import.rb:7
(rdbg) p sku
=> "D4"
(rdbg) kill!
frame 1 moves to the caller to find out which row was bad. kill! ends the program immediately (kill asks for confirmation first, and quit/q exits the debugger and the program).
Catchpoints also accept a superclass (catch StandardError) and if:/do:/pre: like breakpoints.
Note: the debugger ignores exceptions raised from Ruby's internal code (paths like <internal:kernel>). In Ruby 4.0 Integer() and Float() are implemented that way, so catch ArgumentError does not stop on Integer("x"). That's why the example raises its own exception. Exceptions raised by methods written in C (Hash#fetch, Array#fetch, String#+) and by your own code are caught normally.
Tracing
Tracers print events while the program keeps running. They're useful when you don't know where to put a breakpoint.
trace line # every line executed
trace call # every method call and return
trace exception # every exception raised, even rescued ones
trace object expr # every call where the object is the receiver or an argument
trace call /regexp/ # only events whose path or method name matches
trace call into: calls.log # write to a file instead of the console
trace # list tracers
trace off # disable all tracers (trace off 0: just tracer #0)
A plain trace call includes calls into core methods like Integer#+, so it gets long fast. A pattern keeps it readable. On the Account program:
(rdbg) trace call /deposit|withdraw/
Enable CallTracer (enabled) with pattern /deposit|withdraw/
(rdbg) c
DEBUGGER (trace/call) #th:1 #depth:4 > Account#deposit at account.rb:8
DEBUGGER (trace/call) #th:1 #depth:4 < Account#deposit #=> 120 at account.rb:10
DEBUGGER (trace/call) #th:1 #depth:4 > Account#deposit at account.rb:8
DEBUGGER (trace/call) #th:1 #depth:4 < Account#deposit #=> 115 at account.rb:10
DEBUGGER (trace/call) #th:1 #depth:4 > Account#deposit at account.rb:8
DEBUGGER (trace/call) #th:1 #depth:4 < Account#deposit #=> 165 at account.rb:10
DEBUGGER (trace/call) #th:1 #depth:2 > Account#withdraw at account.rb:12
DEBUGGER (trace/call) #th:1 #depth:2 < Account#withdraw #=> 135 at account.rb:14
135
> marks a call and < a return with its value, and the indentation follows the stack depth. trace exception is handy for finding exceptions that some code rescues and swallows silently.
Scripting the debugger
Commands can be given on the command line with -e (repeatable) or in a file with -x. They run at the first stop:
$ rdbg -e 'break Cart#total do: p subtotal' -e 'continue' cart.rb
[1, 4] in cart.rb
=> 1| class Cart
2| Item = Data.define(:name, :price, :qty)
3|
4| def initialize
=>#0 <main> at cart.rb:1
(rdbg:commands) break Cart#total do: p subtotal
Unknown constant name: "Cart"
#0 BP - Method (pending) Cart#total do: p subtotal
(rdbg:commands) continue
[16, 19] in cart.rb
16|
17| def total(discount_pct)
=> 18| discount = subtotal * discount_pct / 100
19| subtotal - discount
=>#0 Cart#total(discount_pct=10) at cart.rb:18
#1 <main> at cart.rb:25
Stop by #0 BP - Method Cart#total at cart.rb:17 do: p subtotal
(rdbg:break) p subtotal
=> 40
Total: 36
A command file is handy for a set of catchpoints you use often. With -n the commands run when the debugger starts, and the program runs without stopping at the first line:
# catch.rdbg, run with: rdbg -n -x catch.rdbg import.rb
catch ArgumentError do: bt
(rdbg:catch.rdbg) catch ArgumentError do: bt
#0 BP - Catch "ArgumentError"
[1, 4] in import.rb
...
Stop by #0 BP - Catch "ArgumentError"
(rdbg:catch) bt
=>#0 Object#parse_qty(str="x") at import.rb:2
#1 block {|sku="D4", qty="x"|} in Object#import at import.rb:7
#2 [C] Array#each at #<none>:0
#3 [C] Enumerable#filter_map at #<none>:0
#4 Object#import(rows=[["A1", "2"], ["B2", "10"], ["C3", "1"],...) at import.rb:6
#5 <main> at import.rb:12
import.rb:2:in 'Object#parse_qty': bad quantity: "x" (ArgumentError)
...
Commands you want in every session go in ~/.rdbgrc (~/.rdbgrc.rb for Ruby code). --no-rc skips it.
Configuration
Every setting can be set with an environment variable, in ~/.rdbgrc with config set name value, or at the prompt. config with no argument lists them all. Some useful ones:
| Setting | Environment variable | Meaning |
|---|---|---|
show_src_lines |
RUBY_DEBUG_SHOW_SRC_LINES |
lines of source shown at a stop (default 10) |
show_frames |
RUBY_DEBUG_SHOW_FRAMES |
frames shown at a stop (default 2) |
no_color |
RUBY_DEBUG_NO_COLOR |
disable colors |
skip_path |
RUBY_DEBUG_SKIP_PATH |
don't stop in or step into these paths (e.g. gem directories) |
postmortem |
RUBY_DEBUG_POSTMORTEM |
open the debugger where an uncaught exception was raised |
irb_console |
RUBY_DEBUG_IRB_CONSOLE |
use IRB as the debugger console |
nonstop |
RUBY_DEBUG_NONSTOP |
don't stop at the start (like -n) |
commands |
RUBY_DEBUG_COMMANDS |
commands to run at the first stop, separated by ;; |
no_reline |
RUBY_DEBUG_NO_RELINE |
read commands with plain gets instead of Reline |
RUBY_DEBUG_NO_RELINE=1 is needed if you pipe commands into the debugger on Windows (rdbg script.rb < commands.txt), where Reline waits for a real console.
Remote debugging: attaching to another process
A program can open a debug port and wait for a client. This is how you debug a server, a background worker, or a program whose terminal you can't type into. TCP is the portable option, and on Linux and macOS a UNIX domain socket is used when no port is given.
# terminal 1: start the program and wait for a client before the first line
rdbg -O --port 12345 cart.rb
# or: run normally and only stop at breakpoints
rdbg -O -n --port 12345 cart.rb
# terminal 2: attach
rdbg -A 12345 # localhost:12345
rdbg -A host 12345 # another host
rdbg -A # UNIX socket: lists waiting processes if there are several
Without rdbg on the server side, require debug/open (wait at start) or debug/open_nonstop (keep running) and choose the port with RUBY_DEBUG_PORT:
require "debug/open_nonstop"
jobs = %w[resize crop rotate watermark]
jobs.each_with_index do |job, i|
puts "processing #{job}"
binding.break if job == "rotate"
end
puts "all jobs done"
Started with RUBY_DEBUG_PORT=54291 ruby worker.rb, the program runs until it reaches the breakpoint and then waits for a client:
DEBUGGER: Debugger can attach via TCP/IP (127.0.0.1:54291)
processing resize
processing crop
processing rotate
DEBUGGER: wait for debugger connection...
Attaching from another terminal:
$ rdbg -A 54291
DEBUGGER (client): Connected. PID:28604, $0:worker.rb
[4, 7] in worker.rb
4| jobs.each_with_index do |job, i|
5| puts "processing #{job}"
=> 6| binding.break if job == "rotate"
7| end
=>#0 block {|job="rotate", i=2|} in <main> at worker.rb:6
#1 [C] Array#each at #<none>:0
# and 2 frames (use `bt' command for all frames)
(rdbg:remote) info locals
%self = main
job = "rotate"
i = 2
jobs = ["resize", "crop", "rotate", "watermark"]
(rdbg:remote) p job.upcase
=> "ROTATE"
(rdbg:remote) continue
The worker then finishes:
DEBUGGER: Connected.
processing watermark
all jobs done
DEBUGGER: Disconnected.
The program's own output stays in its terminal, and the client only shows the debugger. With RUBY_DEBUG_SKIP_BP=1 breakpoints are ignored while no client is attached, instead of making the program wait.
Note: the debug protocol is not encrypted or authenticated. The default host is 127.0.0.1. Don't bind to a public interface (--host 0.0.0.0) on a machine others can reach. Use an SSH tunnel instead. --cookie adds a weak check that the client knows a shared string.
VS Code and other editors
Install the "VSCode rdbg Ruby Debugger" extension (KoichiSasada.vscode-rdbg). It starts rdbg for you and talks to it over the Debug Adapter Protocol, so you get gutter breakpoints, a variables panel, watch expressions and the call stack. A .vscode/launch.json with the two usual configurations:
{
"version": "0.2.0",
"configurations": [
{
"type": "rdbg",
"name": "Debug current file with rdbg",
"request": "launch",
"script": "${file}",
"args": [],
"askParameters": true
},
{
"type": "rdbg",
"name": "Attach with rdbg",
"request": "attach"
}
]
}
For the attach configuration, start the program with rdbg -O --port N script.rb and add "debugPort": "N" to the configuration (without a port, the extension looks for a UNIX domain socket, which works on Linux and macOS). binding.break calls in the code also stop in VS Code. Breakpoint conditions, logpoints and the debug console in VS Code map to if:, do: and expression evaluation in rdbg. rdbg -O --open=chrome script.rb lets you debug in Chrome DevTools instead. Editors with DAP support (Neovim with nvim-dap, Zed, Emacs dap-mode) use the same protocol, and RubyMine has its own debugger.
IRB integration
Recent versions of IRB (the one bundled with Ruby 3.3 and newer) and the debugger are integrated in both directions.
From a binding.irb session, the debug command switches into the debugger at the same point without restarting the program. After that, debugger commands (next, step, info, bt, break, continue) work in IRB:
def price_with_tax(price, rate)
binding.irb
tax = price * rate
(price + tax).round(2)
end
puts price_with_tax(19.99, 0.08)
From: irbdemo.rb @ line 2 :
1: def price_with_tax(price, rate)
=> 2: binding.irb
3: tax = price * rate
4: (price + tax).round(2)
5: end
6:
7: puts price_with_tax(19.99, 0.08)
irb(main):001> price
=> 19.99
irb(main):002> debug
irb:rdbg(main):003> next
[1, 4] in irbdemo.rb
1| def price_with_tax(price, rate)
2| binding.irb
=> 3| tax = price * rate
4| (price + tax).round(2)
=>#0 Object#price_with_tax(price=19.99, rate=0.08) at irbdemo.rb:3
#1 <main> at irbdemo.rb:7
irb:rdbg(main):004> info locals
%self = main
price = 19.99
rate = 0.08
tax = nil
irb:rdbg(main):005> continue
21.59
After debug, the prompt changes from irb to irb:rdbg to show that debugger commands are available.
In the other direction, RUBY_DEBUG_IRB_CONSOLE=1 (or config set irb_console true) makes rdbg and binding.break use IRB as the console. You keep all debugger commands and also get IRB's syntax highlighting, completion and commands such as show_source:
$ RUBY_DEBUG_IRB_CONSOLE=1 rdbg cart.rb
...
irb:rdbg(main):002> break Cart#total
Unknown constant name: "Cart"
#0 BP - Method (pending) Cart#total
irb:rdbg(main):003> continue
...
Stop by #0 BP - Method Cart#total at cart.rb:17
irb:rdbg(#<Cart:0x00000218301a3778>):004> discount_pct * 2
20
irb:rdbg(#<Cart:0x00000218301a3778>):005> show_source Cart#subtotal
From: cart.rb:13
def subtotal
@items.sum { |item| item.price * item.qty }
end
irb:rdbg(#<Cart:0x00000218301a3778>):006> continue
Total: 36
Command reference
| Command | Short | Action |
|---|---|---|
step |
s |
next line, entering calls |
next |
n |
next line in this method |
finish |
fin |
run until the current method returns |
until |
u |
run until a later line (out of a loop) |
continue |
c |
run until the next breakpoint |
break |
b |
add a breakpoint, or list them with no argument |
catch Exc |
stop when Exc is raised |
|
watch @ivar |
wat |
stop when an instance variable changes |
delete N |
del |
remove a breakpoint |
backtrace |
bt |
show the stack |
frame N, up, down |
f |
change the current frame |
info |
i |
show variables |
ls / outline |
o |
show methods and variables |
list |
l |
show more source |
display expr |
evaluate expr at every stop |
|
trace ... |
add tracers | |
irb |
open IRB at the current frame | |
edit |
open the current file in $EDITOR |
|
quit, quit! |
q, q! |
quit (with / without confirmation) |
kill, kill! |
kill the program (with / without confirmation) | |
help [cmd] |
h |
help |
For older tools, see Stepping through code with Pry and Byebug. Byebug's step/next/finish/continue/break/backtrace names carry over to debug, so moving to it mostly means replacing byebug or binding.pry with binding.break.