|
1 | 1 | defrecord IEx.Config, binding: nil, cache: '', counter: 1, scope: nil, result: nil
|
2 | 2 |
|
3 | 3 | defmodule IEx do
|
4 |
| - @moduledoc """ |
5 |
| - This module implements Interactive Elixir. |
| 4 | + @moduledoc %B""" |
| 5 | + Welcome to IEx. |
6 | 6 |
|
7 |
| - The interactive elixir needs to be set as the |
8 |
| - proper `-user` when starting the Erlang VM and |
9 |
| - so can be done with the help of IEx.CLI. |
| 7 | + This module is the main entry point Interactive Elixir and |
| 8 | + in this documentation we will talk a bit about how IEx works. |
10 | 9 |
|
11 |
| - If possible, Elixir will start a tty (smart terminal) |
12 |
| - which makes all control commands available in tty |
13 |
| - available to the developer. |
| 10 | + Notice some of the functionality described here will be available |
| 11 | + depending on your terminal. In particular, if you get a message |
| 12 | + saying that the smart terminal could not be run, some of the |
| 13 | + features described here won't work. |
| 14 | +
|
| 15 | + ## The Break command |
| 16 | +
|
| 17 | + Inside IEx, hitting Ctrl+C will open up the BREAK menu. In this |
| 18 | + menu you can quit the shell, see process and ets tables information |
| 19 | + and much more. |
| 20 | +
|
| 21 | + ## The User Switch command |
| 22 | +
|
| 23 | + Besides the break command, one can type Ctrl+G to get the to |
| 24 | + the user switch command. When reached, you can type `h` to |
| 25 | + get more information. |
| 26 | +
|
| 27 | + In this switch, developers are able to create new shell and |
| 28 | + alternate in between them. Let's give it a try: |
| 29 | +
|
| 30 | + User switch command |
| 31 | + --> s 'Elixir-IEx' |
| 32 | + --> c |
| 33 | +
|
| 34 | + The command above will start a new shell and connect to it. |
| 35 | + Create a new variable called hello and assign some value to it: |
| 36 | +
|
| 37 | + hello = :world |
| 38 | +
|
| 39 | + Now, let's rollback to the first shell: |
| 40 | +
|
| 41 | + User switch command |
| 42 | + --> c 1 |
| 43 | +
|
| 44 | + Now, try to access the hello variable again: |
| 45 | +
|
| 46 | + hello |
| 47 | + ** (UndefinedFunctionError) undefined function: IEx.Helpers.hello/0 |
| 48 | +
|
| 49 | + The command above fails because we have changed the shells |
| 50 | + and they are isolated from each other, you can access the |
| 51 | + variables defined in one in the other. |
| 52 | +
|
| 53 | + The User Switch also allow developers to connect to remote |
| 54 | + shells using r. Keep in mind that you can't connect to a |
| 55 | + remote node if you haven't given a name to the current node |
| 56 | + (i.e. Process.is_alive? must return true). |
| 57 | +
|
| 58 | + ## Expressions in IEx |
| 59 | +
|
| 60 | + As an interactive shell, IEx evalutes expressions. This has some |
| 61 | + interesting consequences worthy discussing. |
| 62 | +
|
| 63 | + The first one is that the code is truly evaluated and not compiled. |
| 64 | + This means that, any benchmarking done in the shell is going to have |
| 65 | + skewed results. So never run any profiling nor benchmark in the shell. |
| 66 | +
|
| 67 | + Second of all, IEx alows you to break an expression into many lines, |
| 68 | + since this is common in Elixir. For example: |
| 69 | +
|
| 70 | + iex(1)> "ab |
| 71 | + ...(1)> c" |
| 72 | + "ab\nc" |
| 73 | +
|
| 74 | + In the example above, the shell will be expecting more input until it |
| 75 | + finds the closing quote. Sometimes it is not obvious which character |
| 76 | + the shell is expecting, and the user may find themselves trapped in |
| 77 | + the state of incomplete expression with no ability to terminate it other |
| 78 | + than by exiting the shell. |
| 79 | +
|
| 80 | + For such cases, there is a special break-trigger ("#iex:break") that when |
| 81 | + encountered on a line by itself will force the shell to break out of any |
| 82 | + pending expression and return to its normal state: |
| 83 | +
|
| 84 | + iex(1)> ["ab |
| 85 | + ...(1)> c" |
| 86 | + ...(1)> " |
| 87 | + ...(1)> ] |
| 88 | + ...(1)> #iex:break |
| 89 | + ** (TokenMissingError) iex:1: incomplete expression |
14 | 90 |
|
15 |
| - In case `tty` is not available (for example, Windows), |
16 |
| - a dumb terminal version is started instead. |
17 | 91 | """
|
18 | 92 |
|
19 | 93 | @doc """
|
|
0 commit comments