Mix.install([
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:tested_cell, github: "brooklinjazz/tested_cell"},
{:utils, path: "#{__DIR__}/../utils"}
])
Ensure you type the ea
keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.
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.
Execute some simple arithmetic in the iex
shell, like so.
iex > 4 + 4
8
You can break your Elixir code into multiple lines in the IEx shell, which only evaluates when the expression is complete.
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
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!"
IEx is often useful for small experiments, running Elixir scripts, and even as a playground environment for an application.
Once we've started the IEx shell, you can close it by pressing CTRL+\.
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)
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
The built-in IEx.Helpers module provides helpers to make it easier to work with the IEx shell.
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
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
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()
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!"
end
end
IO.puts("Executing hello.exs")
Now, recompile the module and execute the hello/0
function.
iex> r Greeting
iex> Greeting.hello()
"Hello!"
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"}
if the IEx shell is getting messy, we can use the clear helper to clear the console screen.
iex> clear
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
.
Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish iex section"
Previous | Next |
---|---|
Phone Number Parsing | Mix |