You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/elixir/pages/getting-started/introduction.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,4 +57,4 @@ $ elixir simple.exs
57
57
Hello world from Elixir
58
58
```
59
59
60
-
Later on we will learn [how to compile Elixir code](modules-and-functions.md)and how to createand work within Elixir projects using the Mix build tool. For now, let's move on to learn the basic data types in the language.
60
+
`iex` and `elixir` are all we need to learn the main language concepts. There is a separate guide named ["Mix and OTP guide"](../mix-and-otp/introduction-to-mix.md)that explores how to actually create, manage, and test full-blown Elixir projects. For now, let's move on to learn the basic data types in the language.
Copy file name to clipboardExpand all lines: lib/elixir/pages/getting-started/module-attributes.md
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ In the example above, we are defining the module documentation by using the modu
32
32
33
33
`@moduledoc` and `@doc` are by far the most used attributes, and we expect you to use them a lot. Elixir treats documentation as first-class and provides many functions to access documentation. We will cover them [in their own chapter](writing-documentation.md).
34
34
35
-
Let's go back to the `Math` module defined in the previous chapters, add some documentation and save it to the `math.ex` file:
35
+
Documentation is only accessible from compiled modules. So in order to give it a try, let's once again define the `Math` module, but this time within a file named `math.ex`:
36
36
37
37
```elixir
38
38
defmoduleMathdo
@@ -53,23 +53,29 @@ defmodule Math do
53
53
end
54
54
```
55
55
56
-
Elixir promotes the use of Markdown with heredocs to write readable documentation. Heredocs are multi-line strings, they start and end with triple double-quotes, keeping the formatting of the inner text. We can access the documentation of any compiled module directly from IEx:
56
+
Elixir promotes the use of Markdown with heredocs to write readable documentation. Heredocs are multi-line strings, they start and end with triple double-quotes, keeping the formatting of the inner text.
57
57
58
-
```console
59
-
$ elixirc math.ex
60
-
$ iex
58
+
Now let's compile it. Start `iex` and then invoke [the `c/2` helper](`IEx.Helpers.c/2`):
59
+
60
+
```elixir
61
+
iex>c("math.ex", ".")
62
+
[Math]
61
63
```
62
64
65
+
And now we can access them:
66
+
63
67
```elixir
64
-
iex> h Math#Access the docs for the module Math
68
+
iex> h Math#Docs for module Math
65
69
...
66
-
iex> h Math.sum#Access the docs for the sum function
70
+
iex> h Math.sum#Docs for the sum function
67
71
...
68
72
```
69
73
70
-
We also provide a tool called [ExDoc](https://github.com/elixir-lang/ex_doc) which is used to generate HTML pages from the documentation.
74
+
When we compiled the module, you may have noticed Elixir created a `Elixir.Math.beam` file. That's the bytecode for the module and that's where the documentation is stored.
75
+
76
+
In our day to day, Elixir developers use the `Mix` build tool to compile code and projects like [ExDoc](https://github.com/elixir-lang/ex_doc) to generate HTML and EPUB pages from the documentation.
71
77
72
-
You can take a look at the docs for `Module` for a complete list of supported attributes. Elixir also uses attributes to annotate our code with [typespecs](../references/typespecs.md).
78
+
Take a look at the docs for `Module` for a complete list of supported attributes.
Copy file name to clipboardExpand all lines: lib/elixir/pages/getting-started/modules-and-functions.md
+9-36Lines changed: 9 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,38 +25,13 @@ iex> Math.sum(1, 2)
25
25
3
26
26
```
27
27
28
-
In this chapter we will define our own modules, with different levels of complexity. As our examples get longer in size, it can be tricky to type them all in the shell. It's about time for us to learn how to compile Elixir code and also how to run Elixir scripts.
28
+
In this chapter we will define our own modules, with different levels of complexity. As our examples get longer in size, it can be tricky to type them all in the shell, so we will resort more frequently to scripting.
29
29
30
-
## Compilation
30
+
## Scripting
31
31
32
-
Most of the time it is convenient to write modules into files so they can be compiled and reused. Let's assume we have a file named `math.ex` with the following contents:
32
+
Elixir has two file extensions `.ex` (Elixir) and `.exs` (Elixir scripts). Elixir treats both files exactly the same way, the only difference is in intention. `.ex` files are meant to be compiled while `.exs` files are used for scripting.
33
33
34
-
```elixir
35
-
defmoduleMathdo
36
-
defsum(a, b) do
37
-
a + b
38
-
end
39
-
end
40
-
```
41
-
42
-
This file can be compiled using `elixirc`:
43
-
44
-
```console
45
-
$ elixirc math.ex
46
-
```
47
-
48
-
This will generate a file named `Elixir.Math.beam` containing the bytecode for the defined module. If we start `iex` again, our module definition will be available (provided that `iex` is started in the same directory the bytecode file is in):
49
-
50
-
```elixir
51
-
iex>Math.sum(1, 2)
52
-
3
53
-
```
54
-
55
-
## Scripting mode
56
-
57
-
In addition to the Elixir file extension `.ex`, Elixir also supports `.exs` files for scripting. Elixir treats both files exactly the same way, the only difference is in intention. `.ex` files are meant to be compiled while `.exs` files are used for scripting. This convention is followed by projects like `mix`.
58
-
59
-
For instance, we can create a file called `math.exs`:
34
+
Let's create a file named `math.exs`:
60
35
61
36
```elixir
62
37
defmoduleMathdo
@@ -74,15 +49,13 @@ And execute it as:
74
49
$ elixir math.exs
75
50
```
76
51
77
-
Because we used `elixir` instead of `elixirc`, the module was compiled and loaded into memory, but no `.beam` file was written to disk.
78
-
79
-
Elixir projects are usually organized into three directories:
52
+
You can also load the file within `iex` by running:
When working on actual projects, the build tool called `mix` will be responsible for compiling and setting up the proper paths for you. For learning and convenience purposes, we recommend you to write the following code into script files and execute them as shown above.
0 commit comments