Getting help and accessing documentation within IEx
Documentation is a first-class citizen in the Elixir ecosystem, so it comes as no surprise that IEx provides convenient ways to access documentation and get help without the need to leave an IEx session.
This recipe exemplifies the use of the defined help functions.
How to do it…
We will follow these steps to get help and access documentation in an IEx session:
- Enter
h
inside a running IEx session to see the help options related to the use of IEx helpers, as shown in this screenshot: - If we wish, for instance, to get information regarding the
c/2
function, we typeh(c/2)
, as shown in the following screenshot: - Accessing a module documentation is done by invoking
h(ModuleName)
. In the next screenshot, we access documentation related toEnum
: - Getting information about a specific function inside a module is also possible by invoking
h(ModuleName.function_name)
. The following screenshot shows the documentation forEnum.map
:
How it works…
When we define modules, it is possible to use the @moduledoc
and @doc
annotations to define documentation related to the whole module or to a specific function in that module.
IEx parses the documentation defined with these annotations and makes it available in a convenient way so that there's no need to leave the session when help or some more information is needed.
IEx itself has several helper functions defined (refer to the first screenshot of this recipe), and among them, we find h/0
and h/1
.
Note
It is common to refer to functions by their name followed by /
and a number indicating the number of arguments that function takes. Therefore, h/0
is a function named h
that takes 0
arguments, and h/1
is the same h
function but with 1
argument.
There's more…
There are several defined functions that allow accessing information on function specifications and types (if defined). To learn more, you can use s/1
and t/1
.
As an example, to get information on the types defined for the Enum
module, we would use t(Enum)
, and to get information on the specifications, we would use s(Enum)
.