DEV Community

Carlos Ramírez
Carlos Ramírez

Posted on

iex

Welcome back to my series on Elixir! In this post, we'll be exploring one of the most powerful tools in the Elixir developer's arsenal: iex. Short for "Interactive Elixir," iex is a command-line interface that allows you to interact with the Elixir language in real-time, making it an indispensable tool for debugging, testing, and exploring the capabilities of the language.

Whether you're a seasoned Elixir developer or just getting started, iex is a tool that you'll want to have in your toolkit. In this post, we'll be taking a deep dive into what iex is, what it's used for, and how you can use it to supercharge your Elixir development workflow. So grab a cup of coffee, fire up your terminal, and let's dive in!

As we saw previously, to start iex you just need to type iex in your terminal:

iex
Erlang/OTP 24 [erts-12.0.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Interactive Elixir (1.13.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
Enter fullscreen mode Exit fullscreen mode

As you can see, it will show you the Erlang and Elixir version you are currently using. You can easily change this using asdf command as we saw previously.

You can type h to see the commands available in the terminal:

iex(1)> 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).

You can use the h/1 function to invoke the documentation for any Elixir module
or function:

    iex> h(Enum)
    iex> h(Enum.map)
    iex> h(Enum.reverse/1)

You can also use the i/1 function to introspect any value you have in the
shell:

    iex> i("hello")

There are many other helpers available, here are some examples:

  • b/1            - prints callbacks info and docs for a given module
  • c/1            - compiles a file
  • c/2            - compiles a file and writes bytecode to the given path
...
...
...
Enter fullscreen mode Exit fullscreen mode

Here, you can also work with Elixir data types and functions:

iex(2)> hello = :world
:world
iex(3)> foo = "bar"
"bar"
iex(4)> one = 1
1
iex(5)> print = fn text -> IO.puts text end
#Function<44.40011524/1 in :erl_eval.expr/5>
iex(6)> print.("Hey there")
Hey there
:ok
Enter fullscreen mode Exit fullscreen mode

As you can see we can define variables, functions and execute those functions (we're going to deep dive into this in a later post)

In this command line you can also compile Elixir files. This is super hyper mega important as you will see in my future posts how we can use iex as a tool in our development process, not only for compiling Elixir files but complete applications.

In conclusion iex is an interactive shell that allows you to execute Elixir code and interact with the language in real-time. This is going to be one of your best friends in your career as an Elixir Developer.


Thanks for reading! My goal is to make Elixir accessible and easy to understand for everyone. If you have any questions or feedback, please don't hesitate to reach out. And be sure to check back regularly for more articles on https://dev.to/calbertora, as I strive to make each post as clear and readable as possible.

Top comments (0)