{id: introduction}
{id: crystal-language}
- Crystal lang
- Syntax similar to Ruby
- LLVM under the hood
- Ideas from Go, Erlang, Rust, Swift
- Statically type checked
- Built-in type inference
- Types are non-nilable (compile time check for lack of assignment)
- Meta-programming with Macro
- Concurrency with green threads called fibers
- C-bindings
{id: crystal-shards} {i: shards}
- The 3rd party modules directory
- Shardbox
- Crystal Shards
- Use
shards init
to create a file calledshard.yml
and describe the dependencies there.
{id: install-shards}
- Create a file called
shard.yml
for your project listing the dependencies of this project - Run
shards install
{id: install-crystal}
{id: docker-on-linux}
- Docker Crystal
- Install Docker
- Create a file called
crystal
with the following content:
- Make it executable by running
chmod +x crystal
- Run
./crystal examples/intro/hello_world.cr
Alternative:
Start docker container:
docker run --rm -it -w/opt -v$(pwd):/opt --name crystal -d crystallang/crystal tail -f /opt/Dockerfile
Execute in the running container:
docker exec crystal crystal examples/intro/hello_world.cr
Later you can stop the container:
docker -t0 stop crystal
{id: hello-world} {i: puts}
puts
stands for "put string"- Adds a newline at the end
{id: run-crystal-from-source-code}
crystal hello_world.cr
{id: compile-crystal-to-executable}
crystal build hello_world.cr -o hello
./hello
{id: speed-of-crystal}
$ time crystal hello_world.cr
Hello World!
real 0m1.108s
user 0m1.268s
sys 0m0.271s
$ time crystal build hello_world.cr -o hw
real 0m1.108s
user 0m1.323s
sys 0m0.238s
$ time ./hw
Hello World!
real 0m0.013s
user 0m0.001s
sys 0m0.011s
{id: hello-world-print} {i: print}
print
does not add a newline- You can add one by including
\n
{id: hello-name}
print
will include an empty string between its parametersputs
will include a newline between its parameters
{id: hello-name-interpolation} {i: #}
- Interpolation is the embedding of variables in strings using
#{}
{id: interpolation} {i: #}
- Interpolation using
#{}
for strings, integers, floating point numbers - and even expressions.
{id: escaping} {i: %}
{id: debugging-print} {i: p!}
p!
can be useful, especially for debugging prints as it includes the name of the variable.- Here we see a simple string printed and then a slightly more complex data-structure.
{id: comments}
{id: code-formatting}
-
2-space indentation is the norm.
-
Use the Crystal tool to format your code:
crystal tool format
- You can also use it in a CI system to verify code-formatting
- (You can also make the CI format it for you and commit the changes back to the repository)
crystal tool format --check
{id: types} {i: typeof} {i: String} {i: Int32} {i: Float64} {i: Bool} {i: p!}
{id: compound-types} {i: typeof} {i: Array} {i: Tuple} {i: Hash} {i: NamedTuple}
{id: add-numbers} {i: +}
- Interpolation works on numbers as well
- The
+
operator is numerical addition or string concatenation
{id: add-mixed-strings-and-integers}
Error: no overload matches 'Int32#+' with type String
{id: numeric-operators}
{id: int-methods} {i: abs} {i: round} {i: even} {i: gcd}
{id: float-methods} {i: abs} {i: round}
{id: program-name} {i: PROGRAM_NAME}
- When running with
crystal examples/intro/program_name.cr
:
We can also compile it
crystal build examples/intro/program_name.cr
-
This will generate
program_name
(as that was the name of our source file) -
We can rename it:
mv program_name other
-
We can run it
./other
and it will print the name of the executable fileother
.
{id: command-line-arguments} {i: ARGV}
- ARGV is an Array of Strings.
Array(String)
{id: exit} {i: exit}
- Exit code defaults to 0
echo $?
echo %ERROR_LEVEL%
{id: rectangle}
{id: true-values}
false
,nil
, and the null pointer are "falsy" everything else, including 0 and "" aretrue
{id: math-pi} {i: PI} {i: Math}
{id: read-from-stdin} {i: gets}
{id: read-numbr-from-stdin}
{id: interactive-environments}
{id: crystal-one-liners} {i: eval}
crystal eval
{id: random-numbers}
{id: exercise-hello-world}
- Install Crystal.
- Verify that you can run it on the command line and print the version number
crystal --version
. - Create a file called
hello_world.cr
that will print out "Hello World"
{id: exercise-hello-name-stdin}
- Create a file called
hello_name_stdin.cr
that will ask the user for their name. - Wait till the user types in their name.
- print "Hello NAME" with their name.
{id: exercise-hello-name-argv}
- Create a file called
hello_name_argv.cr
that expects a name on the command line: crystal hello_name_argv.cr FooBar
- print "Hello FooBar" using whatever name the user provided.
{id: exercise-circle-stdin}
- Create a file called
circle_stdin.cr
that will ask the user for a number. - Then wait till the user types in a number (the radius of a circle).
- Print the area and the circumference of the circle.
{id: exercise-circle-argv}
- Create a file called
circle_argv.cr
that will expect a number on the command line, the radius of a circle. - Print the area and the circumference of the circle.
{id: exercise-calculator-stdin}
- Create a file called
calculator_stdin.cr
- Ask the user for two numbers and an operator
(+, -, *, /)
- Compute the result of the operation and print it out.
{id: exercise-calculator-argv}
- Create a file called
calculator_argv.cr
that the user can run with two numbers and an operator(+, -, *, /)
. crystal calculator_argv.cr 3 + 7
- Compute the result of the operation and print it out.
{id: exercise-age-limit-stdin}
-
Create a script called
age_limit_stdin.cr
-
Ask the user what is their age.
-
If it is above 18, tell them they can legally drink alcohol.
-
If is is above 21, tell them they can also legally drink in the USA.
-
Extra: ask the user for their age and the name of their country and tell them if they can legally drink alcohol.
-
See the Legal drinking age list.
-
Don't worry if this seems to be too difficult to solve in a nice way. We'll learn more tools to improve.