iex: the interactive shell
iex is the interactive shell that comes with Elixir. It reads one expression, evaluates it, prints the result and asks for the next one; nothing is saved to a file. It is the fastest way to check what a function returns before that call goes into a program.
The blocks below that start with a prompt are a session, not a file: the line after iex(1)> is typed, and the line under it is what the shell printed. The check that runs the programs of the course counts a session as skipped, because an interactive shell has no end; the blocks with a file name are scripts, and they are run like every other program of the course.
Start iex
Run iex with no arguments from the folder where you work. The shell prints the version of the runtime, one line of help and the first prompt:
iex
Erlang/OTP 27 [erts-15.2.7.14] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]
Interactive Elixir (1.19.7) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
The number in the prompt counts the expressions of the session, so it moves with every line. The banner names the Erlang runtime and the Elixir version, the same two values that elixir --version prints.
An expression at a time
Type an expression and press Enter. The shell prints the value it computed, not the word nil and not a log line, so the result of every step is visible:
iex> 1 + 2
3
iex> "hi " <> "there"
"hi there"
iex> name = "Ada"
"Ada"
A binding made in one line is there in the next, which makes the shell a scratchpad for a whole session. The same three lines in a file print nothing at all, because a script does not echo values: that is what IO.puts/1 and IO.inspect/1 are for. This script prints the same three values, and its output has the same shape as the session above:
shell.exs
IO.inspect(1 + 2) IO.inspect("hi " <> "there") name = "Ada" IO.inspect(name)
elixir shell.exs
3
"hi there"
"Ada"
IO.inspect/1 writes the value in the same notation the shell uses, quotes and all, which is why it is the usual debugging call of a script. IO.puts/1 writes the text without the quotes, and the next lessons use both.
IO.inspect/2 takes a label as its second argument, which is how a script prints several values in a debug run without a line of prose around each one:
labels.exs
value = {1, 2} IO.inspect(value, label: "tuple") IO.inspect(String.length("hi"), label: "length")
elixir labels.exs
tuple: {1, 2}
length: 2
h, i and Tab
h prints the documentation of a function or a module. h(IO.puts) shows the signature, the spec and the prose that the source of the function carries; the block below is the first sentence of that text, with the rest left out:
iex> h(IO.puts)
Writes `item` to the given `device`, similar to `write/2`, but adds a newline at the end.
...
i is the other helper a reader uses daily: it prints the term, its data type and its size. i("hello") says the term is "hello", the data type is BitString and the byte size is 5, which is the first place the course shows that a string is a binary.
Tab completes a name, and Tab twice lists the candidates. Type Str and press Tab: the shell fills in String. Type String. and press Tab twice to see the functions of the module. The completion reads the same documentation the h helper prints.
Leaving the shell
Press Ctrl+C twice to stop the shell and return to the terminal. From inside a session, System.halt() stops the virtual machine at once, which is the same thing the Ctrl+C sequence does without the question. A session that is stuck in a long computation answers to Ctrl+G, which opens a menu; q there returns to the terminal.
Nothing typed at the prompt is saved. A line worth keeping goes into a file, and the next lesson runs that file with the elixir command.
The mistake: puts at the prompt
The shell evaluates Elixir, not shell commands, and it does not import IO for you. A reader who types the bare name of a function gets a compile error, and the shell keeps the session open:
iex> puts "hi"
error: undefined function puts/1 (there is no such import)
└─ iex:1
** (CompileError) cannot compile code (errors have been logged)
The fix is the full name, IO.puts("hi"). The same rule holds in a script: a function always belongs to a module, and the module is part of the call unless the module was imported first. The lesson on import, alias, require and use shows when an import is worth it.
The sessions and the script 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.
-
What does the iex prompt
iex(3)>tell you?Answer
The next expression is the third of this session
The number in the prompt counts the expressions evaluated in the session.
-
What happens to a value you compute in iex and do not save?
Answer
It is gone when the shell exits
iex keeps a session in memory; a line worth keeping goes into a file.
-
What does
h(IO.puts)do at the prompt?Answer
It prints the documentation of IO.puts/1
h/1 is the help helper of IEx: it prints the docs of the function or module named.
-
What does
i("hello")print?Answer
The term, its data type and its byte size
i/1 is the information helper: it reports the term, its type and its size.
-
What does this program print?
IO.inspect(1 + 2)Answer
3
IO.inspect/1 prints the value it is given, and 1 + 2 is 3.
-
What does this program print?
IO.inspect("hi " <> "there")Answer
"hi there"
IO.inspect/1 prints a binary with its quotes, unlike IO.puts/1.
-
What does this program print?
name = "Ada" IO.inspect(name)Answer
"Ada"
The binding holds the binary "Ada", and inspect prints it in the shell notation.
-
What does this program print?
IO.puts(:ok)Answer
ok
IO.puts/1 writes the text of the atom without the colon or the quotes.
-
What error does
puts "hi"give at the iex prompt?Answer
A CompileError: undefined function puts/1, because IO was not imported
A bare puts/1 does not exist; the function is IO.puts/1 and the module is part of the call.
-
How do you leave a running iex session and return to the terminal?
Answer
Press Ctrl+C twice, or call System.halt()
Ctrl+C twice stops the shell; System.halt/0 stops the VM from inside the session.
Автор статьи: Arthur Isaev