elixirian · book · journey · elixir · functional programming · elixir · iex

Elixirian | Chapter 2 | IEx: Your Interactive Gateway to Elixir

IEx (Interactive Elixir) is more than just a REPL (Read-Eval-Print Loop) —it’s your laboratory, debugger and exploration tool rolled into one.

Whether you’re learning Elixir, debugging production issues, or prototyping solutions, IEx is an indispensable part of the Elixir developer experience.

Starting Your IEx Journey

Launch IEx from your terminal:

$ iex
Erlang/OTP 25 [erts-13.0] [source] [64-bit] [smp:8:8]

Interactive Elixir (1.14.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>


Immediate Feedback Loop:

iex> 1 + 1
2

iex> String.reverse("elixir")
"rixile"

iex> Enum.map([1, 2, 3], &(&1 * 2))
[2, 4, 6]


IEx intelligently handles multi-line expressions:

iex> defmodule Greeter do
...>   def hello(name) do
...>     "Hello, #{name}!"
...>   end
...> end
{:module, Greeter, <<70, 79, 82, ...>>, {:hello, 1}}

iex> Greeter.hello("World")
"Hello, World!"

Built-in Helpers

The h Helper - Your Documentation Companion h helper

i - Inspect Any Value

iex> i "hello"
Term
  "hello"
Data type
  BitString
Byte size
  5
Description
  This is a string: a UTF-8 encoded binary...

or inspect module

i HelloWorld

  • which tells that HelloWorld is an Atom and points to .module_info()

module_info

  • which provides structured metadata about module


t - Type Information

iex> t Enum
@type t() :: Enumerable.t()
@type acc() :: any()
@type element() :: any()


b - Callbacks and Behaviors

iex> b GenServer
@callback init(init_arg :: term()) ::
  {:ok, state}
  | {:ok, state, timeout() | :hibernate | {:continue, continue_arg :: term()}}
  | :ignore
  | {:stop, reason :: any()}
  when state: any()


v() - Accessing Previous Results

iex(1)> 10 * 10
100
iex(2)> 5 * 25
125
iex(3)> v()  # Get last result
125
iex(4)> v(-1) # Same as above
125
iex(5)> v(1)  # Get result from line 1
100
iex(6)> v(2)  # Get result from line 2
125
iex(7)>sum = v(1) + v(2)  # Use results from line 1 and 2 


v(N) accessing previous result

Shell History

# Navigate history with Up/Down arrows
# Search history with Ctrl+R (reverse search)


Recompiling Modules

iex> r MyModule
warning: redefining module MyModule
{:reloaded, MyModule, [MyModule]}


Debugging / Prying into Code
Add breakpoints in your code:

defmodule Calculator do
  def complex_calculation(x, y) do
    require IEx
    
    IEx.pry()
    result = x * y + 10
 
    IEx.pry()
    result / 2
  end
end

When executed, IEx will pause at the pry point: pry-ing

Runtime Inspection

# List all loaded modules
iex> :code.all_loaded()

# Get process info
iex> Process.info(self())

# See all processes
iex> Process.list()


IEx in Development Workflow

Testing Ideas Quickly

iex> users = [%{name: "Alice", age: 30}, %{name: "Bob", age: 25}]
iex> Enum.filter(users, & &1.age > 28)
[%{age: 30, name: "Alice"}]

Exploring External APIs

iex> {:ok, response} = HTTPoison.get("https://api.example.com/data")
iex> Jason.decode!(response.body)

Database Queries

iex> Repo.all(User)
iex> User |> where([u], u.age > 21) |> Repo.all()


IEx Tips and Tricks

1. Auto-completion

Press Tab for auto-completion:

iex> Enum.ma<TAB>
map         map_every   map_intersperse   map_join   map_reduce

2. Exiting IEx

Multiple ways to exit: - Ctrl+C twice - Ctrl+\ - System.halt() - exit()

3. Shell Commands

Run shell commands with :os.cmd:

iex> :os.cmd('ls -la') |> to_string() |> IO.puts()

4. Loading Files

iex> c("path/to/file.ex")  # Compile and load
iex> import_file("path/to/script.exs")  # Run script

5. Benchmarking

iex> :timer.tc(fn -> Enum.sum(1..1_000_000) end)
{15834, 500000500000}  # {microseconds, result}


IEx for Production Debugging

Observer - Visual System Inspection

iex> :observer.start()

This launches a GUI showing: - System overview - Application supervision trees - Process information - ETS tables - Memory usage

Tracing Function Calls

iex> :dbg.tracer()
iex> :dbg.p(:all, :c)
iex> :dbg.tpl(String, :upcase, [])

Common IEx Patterns

Creating Test Data

iex> users = for i <- 1..10, do: %{id: i, name: "User #{i}"}
iex> Enum.take_random(users, 3)

Quick Module Testing

iex> defmodule QuickTest do
...>   def double(x), do: x * 2
...> end
iex> QuickTest.double(21)
42

Exploring Libraries

iex> h Phoenix.Router
iex> exports Phoenix.Router

Conclusion

IEx transforms the development experience from write-compile-run cycles to immediate, interactive exploration. It’s not just a REPL—it’s your companion for:

  • Learning and experimenting
  • Debugging and troubleshooting
  • Testing and prototyping
  • Production system inspection
  • Interactive documentation browsing

Master IEx, and you’ll find yourself more productive, gaining deeper insights into your code and the Elixir ecosystem.
Whether you’re a beginner exploring syntax or an expert debugging distributed systems, IEx has tools to help you succeed.

When in doubt, fire up IEx and experiment.
The immediate feedback loop accelerates learning and problem-solving like nothing else.


fin