Use IO.inspect
or IEx.pry
:
# inspect [1, 2, 3] |> IO.inspect(label: "before") |> Enum.map(&(&1 * 2)) |> IO.inspect(label: "after") |> Enum.sum # pry def some_fun(a, b, c) do require IEx; IEx.pry ... end
Other resources can be found at https://elixir-lang.org/getting-started/debugging.html
What makes IO.inspect(item, opts \\ [])
really useful in debugging is that it returns the item argument passed to it without affecting the behavior of the original code.
(1..10) |> IO.inspect |> Enum.map(fn x -> x * 2 end) |> IO.inspect |> Enum.sum |> IO.inspect [1, 2, 3] |> IO.inspect(label: "before") |> Enum.map(&(&1 * 2)) |> IO.inspect(label: "after") |> Enum.sum # 3 def some_fun(a, b, c) do IO.inspect binding() ... end
Outputs:
# 1 1..10 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] 110 # 2 before: [1, 2, 3] after: [2, 4, 6] # 3 [a: :foo, b: "bar", c: :baz]
Similar to Ruby's binding.pry
. Will open an interactive shell.
def some_fun(a, b, c) do require IEx; IEx.pry ... end