Skip to content

Commit eead8de

Browse files
authored
Add Kernel.dbg/0 which debugs binding/0 (#12009)
1 parent 3ae82c9 commit eead8de

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5806,6 +5806,8 @@ defmodule Kernel do
58065806
|> String.split() #=> ["Elixir", "is", "cool"]
58075807
|> List.first() #=> "Elixir"
58085808
5809+
With no arguments, `dbg()` debugs information about the current binding. See `binding/1`.
5810+
58095811
## Configuring the debug function
58105812
58115813
One of the benefits of `dbg/2` is that its debugging logic is configurable,
@@ -5846,7 +5848,7 @@ defmodule Kernel do
58465848
are inspected. They are the same options accepted by `inspect/2`.
58475849
"""
58485850
@doc since: "1.14.0"
5849-
defmacro dbg(code, options \\ []) do
5851+
defmacro dbg(code \\ quote(do: binding()), options \\ []) do
58505852
{mod, fun, args} = Application.compile_env!(__CALLER__, :elixir, :dbg_callback)
58515853
apply(mod, fun, [code, options, __CALLER__ | args])
58525854
end

lib/elixir/test/elixir/kernel_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,5 +1480,16 @@ defmodule KernelTest do
14801480
assert output =~ "[:foo, :foo, :foo]"
14811481
refute output =~ "\\e["
14821482
end
1483+
1484+
test "prints binding() if no arguments are given" do
1485+
my_var = 1
1486+
my_other_var = :foo
1487+
1488+
output = capture_io(fn -> dbg() end)
1489+
1490+
assert output =~ "binding()"
1491+
assert output =~ "my_var:"
1492+
assert output =~ "my_other_var:"
1493+
end
14831494
end
14841495
end

0 commit comments

Comments
 (0)