IEx
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue Code EditorsCommand Line Family TreeIEx
IEx is Elixir’s interactive shell. If you install Elixir on your computer, you can run iex
in your command
line to start the interactive shell. It will print information about your IEx environment, such as what Elixir and Erlang/OTP version
you have installed.
$ iex
Erlang/OTP 24 [erts-12.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
Interactive Elixir (1.13.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
We can evaluate Elixir code inside of the IEx shell. IEx is often useful for small experiments, running Elixir scripts, and even as a playground environment building/running Elixir applications.
Your Turn
Execute some simple arithmetic in the iex
shell, like so.
iex > 4 + 4
8
IO
We can use the IO module for retrieving user input and displaying output. We’ll learn more about modules in later lessons.
Wrap any value in IO.inspect()
to print it.
IO.inspect(2 + 2)
We can also use IO.gets/2 to retrieve input from the user.
iex> input = IO.gets("Give me some input: ")
Give me some input: sure!
"sure!\n"
iex> input
"sure!\n"
Keep in mind, this will work when running an Elixir project or in the IEx shell, but will fail with an :enotsup
(error not supported) error when we run it from Livebook.
IO.gets("I will fail!")
That’s because in Livebook there is no command line or other device to retrieve the user input from.
Your Turn
Run IO.gets/2 in the command line from the IEx shell and bind the return value to an input
variable.
iex> input = IO.gets("Give me some input: ")
Multiline Expressions
You can break your Elixir code into multiple lines in the IEx shell, which only evaluates when the expression is complete.
Your Turn
Write 4 +
in the IEx shell, then press enter to break the expression into multiple lines. Then finish of the expression by writing another integer such as 5
.
iex >
4 +
... > 5
9
Your Turn
Create a module Greeting
in the IEx shell, then call the hello/0
function.
iex> defmodule Greeting do
iex> def hello do
iex> "Hello, world!"
iex> end
iex> end
{:module, Greeting,
<<70, 79, 82, 49, 0, 0, 4, 204, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 140,
0, 0, 0, 14, 15, 69, 108, 105, 120, 105, 114, 46, 71, 114, 101, 101, 116,
105, 110, 103, 8, 95, 95, 105, 110, 102, 111, ...>>, {:hello, 0}}
iex> Greeting.hello()
"Hello, world!"
We’ll learn more about modules in future lessons.
Closing The IEx Shell
Once we’ve started the IEx shell, you can close it by pressing CTRL+\.
Your Turn
First, define a variable in your current IEx shell.
iex> hello = "world"
Close the current IEx shell, then reopen it with iex
command.
Notice that previously defined variables from the previous IEx shell do not exist.
iex(1)> hello
** (CompileError) iex:1: undefined function hello/0 (there is no such import)
Autocomplete
Much like in livebook, you can autocomplete Elixir expressions by pressing Tab.
iex> Enum.a
all?/1 all?/2 any?/1 any?/2 at/2 at/3
IEx Helpers
The built-in IEx.Helpers module provides helpers to make it easier to work with the IEx shell.
Help (h)
The h
helper prints the documentation for any code.
For example, execute the following in your IEx shell to view the Enum documentation.
iex> h Enum
Information (i)
The i
helper provides information about any Elixir term (data type).
For example, execute the following in your IEx shell to view more information about the Integer data type.
iex> i 2
Term
2
Data type
Integer
Reference modules
Integer
Implemented protocols
IEx.Info, Inspect, List.Chars, String.Chars
Compile (c)
The c helper can load an Elixir file into your IEx shell.
For example, create a hello.exs
file with your command line. Add the following contents to
the file with your code editor.
defmodule Greeting do
def hello do
"Hello, world!"
end
end
IO.puts("Executing hello.exs")
Then start an IEx session and load the file.
Notice that IO.puts("Executing hello.exs")
runs immediately.
iex> c "hello.exs"
Executing hello.exs
[Greeting]
The Greeting
module is now available in the current IEx session.
iex> Greeting.hello()
"Hello, world!"
The iex
command also accepts a filename as an argument to load a file into the IEx shell.
$ iex hello.exs
iex> Greeting.hello()
Recompile (r)
The r helper recompiles a module in the current IEx shell.
For example, change the Greeting
module in hello.exs
to return a different value.
defmodule Greeting do
def hello do
"Hello!" # changed value
end
end
IO.puts("Executing hello.exs")
Now, recompile the module and execute the hello/0
function.
iex> r Greeting
iex> Greeting.hello()
"Hello!"
Value (v)
The v helper returns the value on a given line.
We’ve omitted the line numbers in the IEx shell in previous examples, but you’ll notice each line has a corresponding number.
iex(1)> 1 + 1
2
To use the return value from a previous expression, use v/1
with the desired line number.
iex(2)> v(1) + 2
4
Alternatively, we can pipe (|>)
the previous return value into the current expression.
iex(1)> %{}
iex(2)> |> Map.put(:key, "value")
%{key: "value"}
clear
if the IEx shell is getting messy, we can use the clear helper to clear the console screen.
iex> clear
Further Reading
IEx is generally helpful for experimenting with Elixir code and exploring an existing project or script. For more, consider reading the following:
- HexDocs: IEx.Helpers includes a full list of helper functions and their usage beyond those covered above.
-
Elixir Schools: IEx.Helpers an Elixir schools lesson on
IEx.Helpers
.
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish IEx reading"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.