Skip to content

Commit

Permalink
Add elixir inspect TIL
Browse files Browse the repository at this point in the history
  • Loading branch information
acdibble committed Apr 20, 2021
1 parent fa133a6 commit f340ecf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!

---

Expand Down Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions elixir/inspect-with-labels.md
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Elixir": [
"Base encoding methods",
"Get last value in IEx",
"Inspect with labels",
"Releases",
"Test coverage",
"Test setup",
Expand Down

0 comments on commit f340ecf

Please sign in to comment.