IEx is an interactive shell that lets us run Elixir code directly in the terminal. With IEx, we can explore Elixir features, read documentation, debug, and do much more. It’s a built-in feature, so we can use it right away without installing anything.
Starting Up and Writing Expressions In IEx#
To start an IEx session, just type iex in your terminal and hit enter:
$ iex Erlang/OTP 26 [erts-14.0] [source] [64-bit] [smp:20:20] [ds:20:20:10]
Interactive Elixir (1.15.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Di sinilah kita mengetikkan ekspresi ElixirelixirThe example above shows what an IEx session looks like when it starts up. IEx prints the runtime versions of Erlang and Elixir. At the end of the line, we’ll see the prompt iex(1)>, where we type our Elixir expressions. The number in parentheses indicates the line number.
Let’s try entering our first expression:
iex(1)> 1 + 1 ❶
2 ❷elixirExplanation:
- ❶ We write a simple Elixir expression:
1 + 1. - ❷ Elixir evaluates it and prints the result,
2, on the next line.
Note: In Elixir, everything is an expression that returns a value. This includes not just functions, but also control structures like if and case.
IEx also supports multi-line expressions, which is useful when writing longer pieces of code:
iex(2)> 2 * ( ❶
...(2)> 3 + 1 ❷
...(2)> ) / 4 ❸
2.0 ❹elixirExplanation:
- ❶ We start writing an expression and hit enter to continue on the next line.
- ❷ The second line continues the expression. Notice the
...(2)>prompt, showing that it’s still part of the same input. - ❸ This is the final part of the expression.
- ❹ The output of the full expression is
2.0.
IEx won’t evaluate anything until the expression is complete. If the expression spans multiple lines, IEx will wait for us to finish typing the full input before evaluating it.
Reading Documentation#
IEx also has a feature for reading help or documentation using the h command:
iex(2)> h
IEx.Helpers
Welcome to Interactive Elixir. You are currently seeing the documentation for
the module IEx.Helpers which provides many helpers to make Elixir's shell more
joyful to work with.
This message was triggered by invoking the helper h(), usually referred to as
h/0 (since it expects 0 arguments).
...... (Dan seterusnya)elixirIf we want to know how to properly print text in IEx using the IO.puts function, we can check out the documentation and examples with the h <function name> command:
iex(3)> h IO.puts
def puts(device \\ :stdio, item)
@spec puts(device(), chardata() | String.Chars.t()) :: :ok
Writes item to the given device, similar to write/2, but adds a newline at the
end.
By default, the device is the standard output. It returns :ok if it succeeds.
## Examples
IO.puts("Hello World!")
#=> Hello World!
...... (Dan seterusnya)elixirThis way, we can easily learn how to use a function just by looking at the docs and examples directly in the shell.
How To Debug#
We can also debug in IEx using the dbg function. This function evaluates an expression and prints the result, showing each step alongside the #=> symbol. It’s super useful for tracking how data is transformed at every stage. Here’s a simple example:
iex(14)> dbg(200 + 20.22)
[iex:14: (file)]
200 + 20.22 #=> 220.22
220.22elixirIn the example above, dbg() prints out the expression and its result, 220.22. This helps us see how a value is evaluated in real time.
This becomes even more useful when we’re working with longer code that uses pipelines, Elixir’s way of chaining function calls using the pipe operator |>. With dbg(), we can inspect each step in the pipeline:
iex(16)> -100 |> div(4) |> abs |> to_string() |> dbg ❶
[iex:16: (file)] ❷
100 #=> -100 ❸
|> div(4) #=> -25 ❹
|> abs #=> 25 ❺
|> to_string() #=> "25" ❻
"25" ❼elixirExplanation:
- ❶ We start by writing a function pipeline. The first value is
-100, which will flow to the next function through the pipe operator|>. Here are the steps:- First,
-100flows through thediv(4)function, resulting in-25. - Next,
-25flows into theabs()function, which produces25. - Then,
25flows into theto_string()function, which converts it to the string"25". - Finally, the
dbg()function shows the intermediate results of each operation.
- First,
- ❷ This is the debug output generated by the
dbg()function. It shows the function pipeline written in step ❶, along with the results of each operation. - ❸ The final output of the entire expression is
"25".
Exit From IEx Session And Display Another Options#
The fastest way to exit an IEx session is by pressing CTRL+C twice. If we want to see other options, press CTRL+C once to put the IEx session into a BREAK state. This will display additional options on the next line:
$ iex
Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads
:1] [jit:ns]
Interactive Elixir (1.17.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
BREAK: (a)bort (A)bort with dump (c)ontinue (p)roc info (i)nfo
(l)oaded (v)ersion (k)ill (D)b-tables (d)istributionelixirThe prompt above shows the IEx session in a BREAK state. We can see several options, such as (a)bort to exit the session (by pressing a), or (c)ontinue to keep the session running (by pressing c).
Now we’ve covered the basics of using IEx, we’ll be using it frequently throughout our Elixir development, and throughout this learning series as well.
References#
- Saša Jurić. (2024). Elixir In Action (3rd ed.). Manning Publications.
- Elixir. (2024, December 11). IEx [Documentation]. https://hexdocs.pm/iex/IEx.html ↗