官术网_书友最值得收藏!

  • Elixir Cookbook
  • Paulo A Pereira
  • 352字
  • 2021-07-23 20:49:35

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:

  1. Enter h inside a running IEx session to see the help options related to the use of IEx helpers, as shown in this screenshot:
    How to do it…
  2. If we wish, for instance, to get information regarding the c/2 function, we type h(c/2), as shown in the following screenshot:
    How to do it…
  3. Accessing a module documentation is done by invoking h(ModuleName). In the next screenshot, we access documentation related to Enum:
    How to do it…
  4. Getting information about a specific function inside a module is also possible by invoking h(ModuleName.function_name). The following screenshot shows the documentation for Enum.map:
    How to do it…

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).

主站蜘蛛池模板: 元氏县| 田林县| 天祝| 会理县| 澎湖县| 乃东县| 大英县| 南岸区| 兴宁市| 南召县| 富顺县| 建始县| 太白县| 保靖县| 通城县| 行唐县| 深水埗区| 裕民县| 马尔康县| 桦南县| 丰原市| 高密市| 治多县| 安陆市| 甘孜| 广东省| 东丰县| 铜梁县| 武清区| 岱山县| 克拉玛依市| 衡阳市| 家居| 盘山县| 临泉县| 浙江省| 江北区| 湖北省| 屏山县| 安平县| 宝丰县|