diff --git a/README.md b/README.md index a9e02c2..8ea1b34 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ others" and "this is something really annoying that took forever to find out and I know I'll come across it again at some point so I want it committed for posterity". -36 TILs and growing! +37 TILs and growing! --- @@ -39,6 +39,7 @@ posterity". - [Base encoding methods](elixir/base-encoding-methods.md) - [Get last value in IEx](elixir/get-last-value-in-iex.md) +- [Inspect with labels](elixir/inspect-with-labels.md) - [Releases](elixir/releases.md) - [Test coverage](elixir/test-coverage.md) - [Test setup](elixir/test-setup.md) diff --git a/elixir/inspect-with-labels.md b/elixir/inspect-with-labels.md new file mode 100644 index 0000000..f4c4b18 --- /dev/null +++ b/elixir/inspect-with-labels.md @@ -0,0 +1,30 @@ +# Inspect with labels + +`IO.inspect/2` has an option for passing a label to be printed in front of the +inspected term: + +```elixir +iex(1)> [1, 2, 3] \ +...(1)> |> IO.inspect(label: "before") \ +...(1)> |> Enum.map(&(&1 * 2)) \ +...(1)> |> IO.inspect(label: "after") \ +...(1)> |> Enum.sum() +before: [1, 2, 3] +after: [2, 4, 6] +12 +``` + +It looks like label can be an atom as well: + +```elixir +iex(2)> defmodule Printer do +...(2)> def print(term) do +...(2)> IO.inspect(term, label: __MODULE__) +...(2)> end +...(2)> end +{:module, Printer, <<...>>, {:print, 1}} +iex(3)> Printer.print(nil) +Elixir.Printer: nil +``` + +[source](https://hexdocs.pm/elixir/IO.html#inspect/2) diff --git a/structure.json b/structure.json index 79d9cff..d91057c 100644 --- a/structure.json +++ b/structure.json @@ -5,6 +5,7 @@ "Elixir": [ "Base encoding methods", "Get last value in IEx", + "Inspect with labels", "Releases", "Test coverage", "Test setup",