Skip to content

Commit 7717087

Browse files
author
José Valim
committed
Improve docs for the IEx module and some error messages as well
1 parent 08e6afe commit 7717087

File tree

3 files changed

+104
-45
lines changed

3 files changed

+104
-45
lines changed

lib/iex/lib/iex.ex

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,93 @@
11
defrecord IEx.Config, binding: nil, cache: '', counter: 1, scope: nil, result: nil
22

33
defmodule IEx do
4-
@moduledoc """
5-
This module implements Interactive Elixir.
4+
@moduledoc %B"""
5+
Welcome to IEx.
66
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.
109
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
1490
15-
In case `tty` is not available (for example, Windows),
16-
a dumb terminal version is started instead.
1791
"""
1892

1993
@doc """

lib/iex/lib/iex/cli.ex

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ defmodule IEx.CLI do
22
@moduledoc false
33

44
@doc """
5-
Starts IEx checking if tty (a termnial emulator) is available or not.
6-
If so, invoke tty, otherwise go with the simple iex.
5+
In order to work properly, IEx needs to be set as the
6+
proper `-user` when starting the Erlang VM and we do so
7+
by pointing exactly to this function.
8+
9+
If possible, Elixir will start a tty (smart terminal)
10+
which makes all control commands available in tty
11+
available to the developer.
12+
13+
In case `tty` is not available (for example, Windows),
14+
a dumb terminal version is started instead.
715
"""
816
def start do
917
if tty_works? do
@@ -36,9 +44,15 @@ defmodule IEx.CLI do
3644
args =
3745
if remote = get_remsh(:init.get_plain_arguments) do
3846
unless is_alive do
39-
raise ArgumentError, message: "In order to use --remsh, you need to name the current node"
47+
function = fn ->
48+
IO.puts(:stderr, "In order to use --remsh, you need to name the current node using --name or --sname. Aborting...")
49+
System.halt(1)
50+
end
51+
52+
{ :erlang, :apply, [function, []] }
53+
else
54+
{ remote, :erlang, :apply, [function, []] }
4055
end
41-
{ remote, :erlang, :apply, [function, []] }
4256
else
4357
{ :erlang, :apply, [function, []] }
4458
end

lib/iex/lib/iex/helpers.ex

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,7 @@ defmodule IEx.Helpers do
3737
h(Enum)
3838
h(Enum.reverse/1)
3939
40-
41-
## A note about expressions in IEx ##
42-
43-
IEx treats incomplete expressions in a special way, allowing one
44-
to spill an expression over multiple lines. For example,
45-
46-
iex(1)> "ab
47-
...(1)> c"
48-
"ab\nc"
49-
50-
In the example above, the shell will be expecting more input until it finds
51-
the closing quote. Sometimes it is not obvious which character the shell is
52-
expecting, and the user may find themselves trapped in the state of
53-
incomplete expression with no ability to terminate it other than by exiting
54-
the shell.
55-
56-
For such cases, there is a special break-trigger ("#iex:break") that when
57-
encountered on a line by itself will force the shell to break out of any
58-
pending expression and return to its normal state:
59-
60-
iex(1)> ["ab
61-
...(1)> c"
62-
...(1)> "
63-
...(1)> ]
64-
...(1)> #iex:break
65-
** (TokenMissingError) iex:1: incomplete expression
66-
...
67-
68-
iex(1)>
69-
40+
To learn more about IEx as a whole, just type `h(IEx)`.
7041
"""
7142

7243
@doc """
@@ -119,7 +90,7 @@ defmodule IEx.Helpers do
11990
end
12091

12192
@doc """
122-
Prints the documentation for IEx.Helpers.
93+
Prints the documentation for `IEx.Helpers`.
12394
"""
12495
def h() do
12596
IEx.Introspection.h(IEx.Helpers)

0 commit comments

Comments
 (0)