Creating an Elixir mix project is really easy. A mix project provides some basic structure, sets up a unit testing framework, and can grow with you.
When Elixir is installed, it registers a
mix command. I create a new project named
playing with the command
mix new playing.
This is what happens:
$ mix new playing * creating README.md * creating .formatter.exs * creating .gitignore * creating mix.exs * creating config * creating config/config.exs * creating lib * creating lib/playing.ex * creating test * creating test/test_helper.exs * creating test/playing_test.exs Your Mix project was created successfully. You can use "mix" to compile it, test it, and more: cd playing mix test Run "mix help" for more commands.
There are two newly created files to pay attention to.
lib/playing.ex– Where you add code to play with.
test/playing_test.exs– Where unit tests go for code you write in
For now, don’t worry about writing any tests. Just know that you can and you have an idea of where it would go.
Run the Project’s Tests
An example test was created for us. To run the tests:
$ cd playing/ $ mix test Compiling 1 file (.ex) Generated playing app .. Finished in 0.07 seconds 1 doctest, 1 test, 0 failures Randomized with seed 293342
Try it on the console to get a feel for it.
Load the Project into IEx
Generating the project also created an example function for you. Let’s call it. We can start an IEx session that loads our project’s code for us and we can play with it. It uses the command
iex -S mix. This tells Elixir to start an interactive IEx session that loads the script (
-S) for our mix project.
$ iex -S mix Erlang/OTP 21 [erts-10.0.6] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] Compiling 1 file (.ex) Generated playing app Interactive Elixir (1.8.1) - press Ctrl+C to exit (type h() ENTER for help) iex(1)>
The generated example function we want to call is
Auto-Complete the Modules and Functions
Play and hit your TAB button. It completes the module name. Hit TAB again and it adds the
.. Hit TAB again and it lists all the functions and other namespaces available from “
Playing.“. Type “
h” and hit TAB.
Execute the command and it returns the response
iex(1)> Playing.hello :world
This is a very convenient feature that helps get where you’re going faster and avoiding typos.
Editing Code and Recompiling
Using your preferred code editor, open the
lib/playing.ex file and let’s add the new function below and save the file.
def something_new do "New!" end
Using my already open and running IEx session, if I try to execute
Playing.something_new it fails because the new function hasn’t been compiled yet. IEx can recompile for us using the
recompile function. You can Auto-Complete to that function too.
iex(2)> recompile Compiling 1 file (.ex) :ok
Now the new function is available and can be run.
iex(3)> Playing.something_new "New!"
You don’t have to
recompile if you don’t want to. You can close the running BEAM instance and start it again.
To close it, hit
CTRL+C. Yes, twice. No, “quit” and “exit” are not recognized commands.
Starting it again is
iex -S mix. It includes any file changes.
Playground is Ready!
Now you have a fun little playground up and going. Enjoy!