Labels come to IO.inspect in Elixir 1.4

Sometimes it’s the little things in a language that can really improve your happiness using it. I tend to use IO.puts/1 and IO.inspect/1 a fair amount when I’m debugging Elixir. I might end up doing something like:

def weird_method(strange_input) do
IO.puts "Strange Input:"
strange_input
|> IO.inspect
|> another_call
|> last_call
end

I puts a label first and then inspect the strange_input to see what’s getting passed to the method. This prints something like:

Strange Input: "Some Strange Input"

As of Elixir 1.4 IO.inspect/2 now accepts a label option so we can do this instead:

def weird_method(strange_input) do
strange_input
|> IO.inspect(label: "Strange Input")
|> another_call
|> last_call
end

This ends up printing the exact same thing as our first example, but it’s so much nicer to be able to remove or easily move just one inspect call instead of managing separate lines, especially when inspecting multiple times inside a pipeline where you’d have to break up your pipe to get the labels in the correct place.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.