By the end of this section, you’ll know:
- The basics of Elixir
- Use the elixir REPL to run elixir code
- Run elixir code from a script
- How to find elixir documentation
We can use the REPL (Read-Eval-Print Loop) command-line tool to write and run simple elixir calculations directly from the command-line. The REPL tool is great for writing simple elixir code, checking on syntax, and understanding how the Elixir run-time works.
Let’s open the Interactive Elixir REPL.
iex into your command line.
iex command (_I_nteractive _E_li_X_ir), which launches the Interactive EliXir command prompt.
>$ iex Erlang/OTP 21 [erts-10.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] Interactive Elixir (1.7.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)>
From this console, we’ll be able to play around and experiment with Elixir. Anytime that you see code prepended by the string
iex> , this means we’re working inside the REPL. Feel free to try it along with us as we go. It’s a good way to get your fingers working through writing Elixir.
h command is the help command. It is very useful to find information the elixir language
h() into the command line
You should see something similar to the following:
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: ...
In this section, we’ll be using a few notations it’s good to know about before we start.
If you see the notation of a name followed by a
/ and number, this indicates a function and it’s number of arguments (arity). For example:
is_integer/1- indicates a function called
Newer versions of IEx have implemented the h/1 command. ‘h’ is a quick access to the docs. Let’s try it with the
h Integer.to_string into command line
iex(4)> h Integer.to_string def to_string(integer) @spec to_string(integer()) :: String.t() Returns a binary which corresponds to the text representation of integer. Inlined by the compiler. ## Examples iex> Integer.to_string(123) "123" iex> Integer.to_string(+456) "456" iex> Integer.to_string(-789) "-789" iex> Integer.to_string(0123) "123"
So as you can see, the
hmethod shows you the arguments the function takes and examples of how it is used. In this case
Integer.to_stringtakes an integer and returns a string.
There is a second function defined by
Integer.to_stringdefined in the docs but we won’t worry about that for now.
To exit the
iex prompt, press
It’s always a great idea to be able to look through documentation when you have a question. Using documentation is often overlooked as a skill and thus, we propose it’s a great idea to start with documentation from the beginning.
There are several different ways to load files into our interactive shell. Using the
c command we can tell our REPL which file to compile.
Let’s create a file names ‘calc.ex’
type the following into the command line:
r command we can recompile.
To see what the
r function does we can use our help command
h r into the terminal
iex(1)> h r def r(module) Recompiles and reloads the given module. Please note that all the modules defined in the same file as module are recompiled and reloaded. This function is meant to be used for development and debugging purposes. Do not depend on it in production code.
We have mentiond the term Arity a few times. Arity means the number of arguments a function takes.
In Elixir when we see a function name it will include the
arity of the function
We can see that the
rem function has an arity of 2.
c or compile function has an arity of 1.
Go on to Elixir Types.
Go back to Why program? Why elixir?.