Your first script
| Hello hello.exs | |
| elixir hello.exs | |
| IO.puts strings | |
| Common beginner mistakes |
Hello hello.exs
Scripts are plain text files with the exs extension. Create a file named hello.exs in an empty folder and put one line inside:
hello.exs
IO.puts("Hello, Elixir")
elixir hello.exs
Run the script by passing its name to the elixir command:
elixir hello.exs
Hello, Elixir
The toolchain reads the file from top to bottom and prints the greeting.
IO.puts strings
IO.puts prints strings, and the <> operator joins two strings into one. Interpolation with #{} drops a value straight into a string:
name = "Ada"
IO.puts("Hello, " <> name)
IO.puts("2 + 2 is #{2 + 2}")
Hello, Ada
2 + 2 is 4
Common beginner mistakes
Three mistakes catch almost every beginner. First, a bare puts fails because Elixir keeps printing inside the IO module, so always write IO.puts.
Second, single quoted text is a charlist rather than a string, so use double quotes for all text in this course. Third, the run command needs the full file name with its extension, or the toolchain finds nothing.
Next: Variables
Article author: Arthur Isaev