Elixir Output and Inspect
Inspect output is a second way to print a value, made for checking what a value really is. IO.inspect shows the value in its written form.
Inspect keeps the quotes on text
IO.puts prints the text Ana on its own. IO.inspect prints the same text with its quotes, as "Ana".
IO.puts("Ana")
IO.inspect("Ana")Numbers print the same both ways
A number has no quotes in either form. The integer and the float print just as they are written.
IO.puts(42)
IO.inspect(42)
IO.inspect(3.5)Inspect tells text from a boolean
The word true written as text prints as true under IO.puts, just like the boolean. Under IO.inspect the text keeps its quotes and the boolean does not.
IO.puts("true")
IO.inspect("true")
IO.inspect(true)Example
name = "Ana"
score = 95
passed = true
IO.puts(name)
IO.inspect(name)
IO.inspect(score)
IO.inspect(passed)Key points
- IO.puts: prints a value in its plain form, so text appears without quotes.
- IO.inspect: prints a value in its written form, so text keeps its double quotes.
- Use IO.inspect when two values would look the same under IO.puts.
- Numbers: IO.inspect and IO.puts print numbers alike, such as 42 and 3.5.
- Booleans: IO.inspect prints true and false just as they are written.
In this example, name holds a string, score holds an integer, and passed holds a boolean. IO.puts prints the string without quotes, and IO.inspect then prints the string with quotes, the score as 95, and the boolean as true.
Note
IO.inspect is a checking tool, so programs use IO.puts for normal output.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does IO.inspect("done") print?