IEx and Your First Program

Exit with Ctrl+C twice (or Ctrl+\), or System.halt().

3.1 IEx basics

Exit with Ctrl+C twice (or Ctrl+\), or System.halt().

Useful: iex -S mix starts IEx inside your project; iex --sname a starts a named node.

$ iex
iex(1)> 1 + 2
3
iex(2)> "hello" <> " world"
"hello world"
iex(3)> h String.upcase        # help for a function
iex(4)> i "hello"              # information about a value
iex(5)> v(2)                   # value of expression 2
iex(6)> c "my_file.ex"         # compile & load a file
iex(7)> r MyModule             # recompile a module
iex(8)> recompile()            # inside a Mix project (iex -S mix)
iex(9)> t Enumerable           # show types
iex(10)> exports String        # list exported functions
iex(11)> runtime_info()
iex(12)> break!/dbg/IEx.pry    # debugging helpers

3.2 Hello World script

IO.puts("Hello, World!")
elixir hello.exs

3.3 Hello World with Mix

Project layout:

mix new hello
cd hello
defmodule Hello do
  def world, do: IO.puts("Hello, World!")
end
mix compile
mix run -e "Hello.world()"
iex -S mix
hello/
├── lib/hello.ex
├── test/hello_test.exs
├── test/test_helper.exs
├── mix.exs
├── .formatter.exs
└── README.md

3.4 Comments

# Single-line comment (the only comment syntax)
@moduledoc "Module documentation"
@doc "Function documentation"