Working with mix project iex Code Snippets Stored in Files
iex can be used for interactively experimenting with elixir. If this experimentation becomes elaborate, it can be useful to store the commands in a file. This post covers working with files that store elixir code snippets. Attention is paid to code that depends on mix project modules.
Software Versions
Instructions
A single line can be run from the command line.
Something more elaborate probably wants to live in a file.
snippets/hello.exs
The above file be run from the command line as is, but it will not have acces to modules in the mix project.
Run the file with mix run if any mix project modules are referenced.
Note that c or Code.load_file can be used to load the file in iex.
Code.load_file can be used to load one file from another file. This is fragile if using relative paths.
snippets/indirect.exs
To run mix code snippets directly from the command line,
add the following file to some place on your path.
For example, $(which mix)-snippet
.
mix-snippet
Make mix-snippet executable.
The mix-snippet shebang can now be used.
snippets/hello-mix-snippet.exs
Code snippets that depend on the mix project can now be executed from the command line.