IO, stdin and stdout

The IO module of Elixir is the whole of the conversation a program has with its terminal: three functions write, one reads, and each takes a device so the same call can write a file or the error stream. The distinction that matters is between IO.puts/1, which prints text, and IO.inspect/2, which prints a value in its source form.

puts, write and inspect

IO.puts/1 writes its argument and a newline, IO.write/1 writes it without one, and IO.inspect/2 writes the inspected value with a newline and returns the value unchanged. The program below uses all three and a label:

puts.exs

IO.puts("text") IO.write("no newline") IO.write("\n") IO.inspect(:atom) IO.inspect("text") IO.inspect(42, label: "answer")

elixir puts.exs

text
no newline
:atom
"text"
answer: 42

The atom prints as :atom under inspect and the string keeps its quotes, which is the difference a debugging session needs. IO.puts/1 of the same values would print atom and text, because it writes the text a reader sees rather than the value the code holds.

The device argument

The first argument of each function is the device, and :stdio is the default. :stderr is the second stream, which a shell shows apart from the output and a pipeline can redirect on its own:

device.exs

IO.puts("to stdout") IO.puts(:stderr, "to stderr")

elixir device.exs

to stdout
to stderr

A program writes its results to the standard output and its problems to the standard error, so a caller that captures the output still sees the warnings. IO.inspect/2 takes the device in the same first position, and IO.puts(:stderr, value) is the shortest way to report a problem from a script.

Reading a line

IO.gets/1 writes its prompt and answers the next line as a binary with the newline, or :eof when the input has ended. The program below answers both cases, and the page runs it with a line piped in:

name.exs

case IO.gets("name? ") do :eof -> IO.puts("no input") data -> IO.puts("hello #{String.trim(data)}") end

printf 'Ada\n' | elixir name.exs

name? hello Ada

The prompt and the answer share a line because the prompt has no newline; the trim removes the newline of the input. A run with no input, as in a pipeline test, answers :eof at once, which is why the clause is there: a program that expects a person must know what it does without one. IO.read/2 reads a whole line count or the rest of the input, and IO.binread/2 reads bytes.

The mistake: puts of a map

IO.puts/1 needs text, which it asks of the String.Chars protocol. A map has no text form, so the call stops with a Protocol.UndefinedError that prints the value it was given:

mistake.exs

IO.puts(%{a: 1})

elixir mistake.exs

** (Protocol.UndefinedError) protocol String.Chars not implemented for Map

Got value:

    %{a: 1}

    (elixir 1.19.7) lib/string/chars.ex:7: String.Chars.impl_for!/1
    (elixir 1.19.7) lib/string/chars.ex:26: String.Chars.to_string/1
    (elixir 1.19.7) lib/io.ex:835: IO.puts/2
    mistake.exs:1: (file)

The message says Map rather than the protocol the caller expected, and the fix is IO.inspect(%{a: 1}), which uses the Inspect protocol that every value implements. A struct may define its own text form by implementing String.Chars, which is how IO.puts of a date prints it.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is the difference between IO.puts/1 and IO.inspect/2?

    Answer

    puts writes text, inspect writes the value in its source form

    puts asks for the text of a value; inspect shows the value the code holds.

  2. Which device is the second stream of a program?

    Answer

    :stderr

    :stderr is the stream a shell shows apart from the output, and it is the first argument of the IO calls.

  3. What does IO.gets/1 answer when the input has ended?

    Answer

    :eof

    The end of the input is the atom :eof, which a program handles as its own case.

  4. Which protocol does IO.puts/1 use to turn a value into text?

    Answer

    String.Chars

    puts asks String.Chars for the text of a value, and a value without an implementation raises.

  5. What does this program print? IO.puts("text")

    Answer

    text

    puts writes the text itself and adds a newline.

  6. What does this program print? IO.inspect("text")

    Answer

    "text"

    inspect shows a binary with its quotes, which is its form in the source.

  7. What does this program print? IO.puts(:atom)

    Answer

    atom

    The text of an atom is its name without the colon.

  8. What does this program print? IO.write("a") IO.write("b") IO.puts("")

    Answer

    ab

    write adds no newline, so the two letters are printed side by side.

  9. What does this program print? IO.inspect(42, label: "answer")

    Answer

    answer: 42

    The label is printed before the inspected value, with a colon.

  10. What does this program do? IO.puts(%{a: 1})

    Answer

    A Protocol.UndefinedError, because a map has no String.Chars implementation

    puts needs text, and a map implements Inspect rather than String.Chars.

Article author: Arthur Isaev