I. Introduction
- Introduction to Elixir
1
Setup Erlang and Elixir2
iEX & scripting- iEX
- Hello world
- Run scripts
3
Modules and functions- Modules
- Public and private functions
To be able to run your code you must need:
- Erlang and the BEAM Virtual Machine
- Elixir installation.
Both considerations are in the official documentation. This official guide depending on your OS is fine for the beginning.
I strongly recommend use this for install different versions of Elixir and Erlang for *nix OS (you can install many tools).
- Please install ASDF locally.
- After this you'll need to add the plugins to install Erlang and Elixir:
asdf plugin add erlang https://github.com/asdf-vm/asdf-erlang.git
asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
- You can list all the versions available to install:
asdf list all erlang
orasdf list all elixir
. - Now you can be able to install Erlang and Elixir (install Erlang before Elixir).
asdf install erlang 26.2.1
asdf install elixir 1.16.0-otp-26
- As you can have many versions installed you can set a version locally or globally with
asdf global elixir 1.16.0-otp-26
. (Use this if you install more than 1)
iEX
- Elixir allow us to have a REPL, it comes from Erlang
erl
, so we have theiex
interactive shell to run our code. - In your command line after install elixir you can type
iex
. - Print your first Hello World:
IO.puts("Hello World from the Elixir Workshop for beginners")
.
Scripting
- To run code using Elixir, you need to create a file
hello_world.exs
. - In this file please save this line:
IO.puts("Hello World - Script Mode")
. - To run this code just type:
elixir hello_world.exs
.
- Create a new file
cake_factory.exs
. - Inside this create a new module called
CakeFactory
. - Please write a function
create_cake/0
that prints a message.
cake_factory.exs
defmodule CakeFactory do
def create_cake() do
IO.puts "Cake Factory creates a new cake!"
end
end
- Open an
iex
session. Charge your script:c "cake_factory.exs"
. (Also you can copy the code and paste in the iex session) - Run your function:
CakeFactory.create_cake()
.