Search⌘ K
AI Features

Create the Playground

Explore building an Elixir application scaffolded with a supervision tree. Learn to use mix to create projects and navigate IEx for interactive testing. Understand compiling and recompiling code dynamically while preparing to develop concurrent features using the Task module.

We’ll create an application called sender and pretend to send emails to real email addresses. Later, we use the Task module to develop some of its functionality.

Project creation

First, let’s use the mix command-line tool to scaffold our new project:

mix new sender --sup

This creates a sender directory with a bunch of files and folders inside. Notice that we also used the --sup argument. This creates an application with a supervision tree. We will learn about supervision trees later in this chapter.

We enter the command below to see the created project’s tree.

The following is the executable command:

Shell
tree sender

Here is the output we get:

sender
├── lib
│   ├── sender
│   │   └── application.ex
│   └── sender.ex
└── mix.exs

2 directories, 3 files

Compile the project

Now, we navigate to sender and enter the interactive Elixir (IEx):

The following are the executable commands:

Shell
cd sender
Shell
iex -S mix

This is the output we get:

root@educative:/# cd sender
root@educative:/sender# iex -S mix
Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit]

Compiling 2 files (.ex)
Generated sender app
Interactive Elixir (1.9.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 

We’ll see some Erlang version information and the message above.

We’re now running the Interactive Elixir shell, also known as IEx. We’ll use it to test code frequently throughout the course. Most of the time, when we make a code change, we can call the special recompile/0 function available in IEx, and the code will reload:

Here is the executable command:

Shell
recompile()

This is the output we get:

iex(1)> recompile() 
:noop

When we haven’t added or changed any code, the function will return just :noop for no operation. Otherwise, it will recompile the code.

In some cases, we may need to restart the IEx shell entirely—for example, when making fundamental application changes in the application.ex file. To restart IEx, we press “Ctrl-C” twice to quit and then run the iex -S mix command again.

Playground

Let’s try out the commands above in the terminal below:

Note: Please run all the “Executable” commands sequentially from top to bottom.

Terminal 1
Terminal
Loading...