Skip to content

Commit

Permalink
crystal
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed May 15, 2021
1 parent c32ec6e commit 9d8911c
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions books.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ aws-lambda/aws.json,aws-lambda,
bash/bash.json,bash,
css/css.json,css,
collab-dev/collab.json,collab-dev,
crystal/crystal.json,crystal,
data-science/data.json,data-science,
dart/dart.json,dart,
digitalocean/do.json,digitalocean,
Expand Down
3 changes: 3 additions & 0 deletions crystal/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM crystallang/crystal:latest
WOR

1 change: 1 addition & 0 deletions crystal/crystal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker run --rm -it -w/opt -v$(pwd):/opt crystallang/crystal crystal $*
12 changes: 12 additions & 0 deletions crystal/crystal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"title": "Crystal Programming",
"author": "Gabor Szabo",
"description": "",
"first_year": "2021",
"copyright_name": "Gabor Szabo",
"copyright_url": "https://szabgab.com/",
"version": "0.01",
"files": [
"introduction.md"
]
}
4 changes: 4 additions & 0 deletions crystal/examples/add_numbers.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x = 23
y = 19
z = x + y
puts z # 42
5 changes: 5 additions & 0 deletions crystal/examples/escaping.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = "Foo Bar"
puts "Hello '#{name}'!"
puts "Hello \"#{name}\"!"
puts %(Hello "#{name}"!) # alternative quotes
puts %{Hello "#{name}"!}
2 changes: 2 additions & 0 deletions crystal/examples/hello_name.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Foo Bar"
puts "Hello " + name + "!"
2 changes: 2 additions & 0 deletions crystal/examples/hello_name_interpolation.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "Foo Bar"
puts "Hello #{name}!"
1 change: 1 addition & 0 deletions crystal/examples/hello_world.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello World!"
3 changes: 3 additions & 0 deletions crystal/examples/math.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p! Math::PI
p! Math::TAU
p! Math::E
3 changes: 3 additions & 0 deletions crystal/examples/math.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Math::PI # => 3.141592653589793
Math::TAU # => 6.283185307179586
Math::E # => 2.718281828459045
7 changes: 7 additions & 0 deletions crystal/examples/number_methods.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x = -5.2
p! x.abs
p! x.round
# p! x.even? # Undefined method even? for Float64

age = 42
p! age.even?
11 changes: 11 additions & 0 deletions crystal/examples/types.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "Foo Bar"
puts typeof(name) # String
p! typeof(name) # typeof(name) => String

age = 42
puts typeof(age) # Int32
p! typeof(age) # typeof(age) => Int32

pi = 3.14
puts typeof(pi) # Float64
p! typeof(pi) # typeof(pi) => Float64
102 changes: 102 additions & 0 deletions crystal/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Introduction
{id: introduction}

## Crystal Language
{id: crystal-language}

* [Crystal lang](https://crystal-lang.org/)
* Similar to Ruby


## Docker on Linux
{id: docker-on-linux}

* [Docker Crystal](https://hub.docker.com/r/crystallang/crystal)
* Install Docker
* Create a file called `crystal` with the following content:

![](crystal)

* Make it executable by running `chmod +x crystal`
* Run `./crystal examples/hello_world.cr`

## Hello World (puts)
{id: hello-world}
{i: puts}

![](examples/hello_world.cr)

* `puts` stands for "put string"


## Hello Name (variables)
{id: hello-name}

![](examples/hello_name.cr)

## Hello Name with interpolation
{id: hello-name-interpolation}

![](examples/hello_name_interpolation.cr)

## Escaping - Alternative delimiters
{id: escaping}

![](examples/escaping.cr)


## Add numbers
{id: add-numbers}

![](examples/add_numbers.cr)

## Types
{id: types}
{i: String}
{i: Int32}
{i: Float64}
{i: p!}

![](examples/types.cr)


## Numeric Operators
{id: numeric-operators}


```
+
-
*
** (exponent)
/
// (floor division)
% (modulus)
```

## Comparision Operators
{id: comparision-operators}

* ==, <, > <=, >=

* Spaceship operator <=> returns -1, 0, or 1


## Math
{id: math}

![](examples/math.cr)
![](examples/math.out)


## Other
{id: other}

* Single quotes vs double quotes

No type-checking?

x = "one"
x = 1
p! x
p! typeof(x)

0 comments on commit 9d8911c

Please sign in to comment.