IceCream View Source
Section
IceCream is a port of the python package of the same name.
Never use IO.inspect()
again
Use IceCream to inspect any Elixir term with an automatically generated label.
Installation
The package can be installed by adding ice_cream
to your list of dependencies in mix.exs
. It should only be added for the dev
and test
environments.
def deps do
[
{:ice_cream, "~> 0.0.5", only: [:dev, :test]}
]
end
The docs can be found at https://hexdocs.pm/ice_cream.
Usage
Use IceCream in place of IO.inspect/2
. So write ic(value)
instead of
IO.inspect(some_value, label: "some_value")
It can also be used inside of pipelines. You can write:
Mix.install([{:ice_cream, path: "./"}])
:ok
defmodule SomeModule do
import IceCream
defp fabricate(data), do: data
defp cleanup(data), do: data
def some_fun(data) do
data
|> ic()
|> fabricate()
|> ic()
|> cleanup()
|> ic()
end
end
SomeModule.some_fun("<data>")
ic| data: "<data>"
ic| fabricate(data): "<data>"
ic| cleanup(fabricate(data)): "<data>"
"<data>"
Instead of:
defmodule SomeModule do
defp fabricate(data), do: data
defp cleanup(data), do: data
def some_fun(data) do
data
|> IO.inspect(label: "data")
|> fabricate()
|> IO.inspect(label: "fabricate")
|> cleanup()
|> IO.inspect(label: "cleanup")
end
end
SomeModule.some_fun("<data>")
data: "<data>"
fabricate: "<data>"
cleanup: "<data>"
"<data>"
If in a function, you can pass :location
and :function
as options
defmodule SomeModule do
import IceCream
def some_fun(data) do
ic(data, function: true, location: true)
end
end
SomeModule.some_fun("<data>")
ic| README.livemd#cell:5 in SomeModule.some_fun/1 data: "<data>"
"<data>"
It also works if you pass in a function call
import IceCream
ic(:math.pow(2, 3))
ic| :math.pow(2, 3): 8.0
8.0
Configuration
Default options are configurable.
In addition to location
and function
, any of the Inspect options can be set, such as :limit
# config/dev.exs
config :ice_cream,
location: true,
function: true,
limit: :infinity