Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jul 2, 2021
1 parent 5e6df77 commit 05ea57c
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 5 deletions.
4 changes: 0 additions & 4 deletions crystal/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@

* Implement the exercises in the Python course
* Code to parse the .md files and check if each one of the files from the examples directory has been used.
* How to install 3rd party modules?
* List of 3rd party modules
* Create a web application

* Write a test for https://github.com/watzon/octokit.cr that will use a secret stored in GitHub Actions to use the API

* Modules
* Classes


* Read shard.yml file in particular
* Web application development?
* Access to SQLite?
Expand Down
1 change: 1 addition & 0 deletions crystal/crystal.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"structs.md",
"regexes.md",
"testing.md",
"modules.md",
"json.md",
"range.md",
"random.md",
Expand Down
7 changes: 7 additions & 0 deletions crystal/examples/functions/splat.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def sum(*numbers : Int32)
puts typeof(numbers) # Tuple(Int32, Int32, Int32)
numbers.sum
end

res = sum(2, 3, 4)
puts res # 9
13 changes: 13 additions & 0 deletions crystal/examples/modules/include.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Tools
def screwdriver
puts "Screwdriver"
end
end

class Device
include Tools
end

x = Device.new
p! x
x.screwdriver
7 changes: 7 additions & 0 deletions crystal/examples/modules/namespace.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module MyProject
class Device
end
end

x = MyProject::Device.new
p! x
19 changes: 19 additions & 0 deletions crystal/examples/other/enums.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
enum Direction : UInt8
Left
Right
Up
Down

def left?
self == Direction::Left
end
end

puts Direction::Left
puts Direction::Left.value
puts Direction::Right
dir = Direction::Left
puts dir.left?

dir = Direction::Right
puts dir.left?
11 changes: 11 additions & 0 deletions crystal/examples/other/int_or_nil.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
text = "Black cat"
idx = text.index("cat")
puts typeof(idx) # (Int32 | Nil)

# puts text[idx]
# Error: no overload matches 'String#[]' with type (Int32 | Nil)

if idx.is_a?(Int32)
puts typeof(idx) # Int32
puts text[idx]
end
3 changes: 3 additions & 0 deletions crystal/examples/other/proc.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
square = ->(x : Int32) { x * x }
puts typeof(square) # Proc(Int32, Int32)
puts square.call(3) # 9
15 changes: 15 additions & 0 deletions crystal/examples/other/type_of_array_elements.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
values = [1, 2, "three", "four"]
puts typeof(values)
puts typeof(values[0])
puts typeof(values[2])
puts ""

values.each { |val|
puts typeof(val)
# puts val.size
# Error: undefined method 'size' for Int32 (compile-time type is (Int32 | String))

if val.is_a?(String)
puts val.size
end
}
5 changes: 5 additions & 0 deletions crystal/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@

![](examples/functions/pass_array_instead_of_individual_values.cr)

## Any number of arguments (splat, *)
{id: any-number-of-arguments}

![](examples/functions/splat.cr)

## Manually separate
{id: manually-separate}

Expand Down
11 changes: 10 additions & 1 deletion crystal/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@
* Concurrency with green threads called fibers
* C-bindings

## Crystal Shards
## Crystal Shards (3rd party libraries)
{id: crystal-shards}
{i: shards}

* The 3rd party modules directory
* [Shardbox](https://shardbox.org/)
* [Crystal Shards](https://crystalshards.xyz/)
* Use `shards init` to create a file called `shard.yml` and describe the dependencies there.

## How to install 3rd party shards?
{id: install-shards}

* Create a file called `shard.yml` for your project listing the dependencies of this project
* Run `shards install`

![](shard.yml)

## Install Crystal
Expand Down Expand Up @@ -291,3 +298,5 @@ echo %ERROR_LEVEL%
{i: gets}

![](examples/intro/gets.cr)


13 changes: 13 additions & 0 deletions crystal/modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Modules
{id: modules}

## Namespace
{id: namespace}

![](examples/modules/namespace.cr)

## Extend class - include module
{id: include-module}

![](examples/modules/include.cr)

27 changes: 27 additions & 0 deletions crystal/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,33 @@ require "some_name" # Find it somewhere (standard library, src directory

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

## Proc
{id: proc}
{i: Proc}

* [Proc](https://crystal-lang.org/api/Proc.html)

![](examples/other/proc.cr)

## Enums
{id: enums}
{i: enum}

![](examples/other/enums.cr)

## Type of array elements
{id: type-of-array-elements}

![](examples/other/type_of_array_elements.cr)

All the elements of an array "inherit" the type from the array

## Int32 or Nil
{id: int32-or-nil}

![](examples/other/int_or_nil.cr)


## Resources
{id: resources}

Expand Down

0 comments on commit 05ea57c

Please sign in to comment.