Compiling a Module

Let’s look at two ways to compile this file and load it into iex. First, if you’re at the command line, you can do this:

 $ iex times.exs
 iex>​ Times.double(4)
 8

Give iex a source file’s name, and it compiles and loads the file before it displays a prompt.

If you’re already in iex, you can use the c helper to compile your file without returning to the command line.

 iex>​ c ​"​​times.exs"
 [Times]
 iex>​ Times.double(4)
 8
 iex>​ Times.double(123)
 246

The line c "times.exs" compiles your source file and loads it into iex. We then call the double function in the Times module a couple of times using Times.double.

What happens if we make our function fail by passing it a string rather than a number?

 iex>​ Times.double(​ ...

Get Programming Elixir 1.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.