Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blocks section to Tutorials #655

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
* [Strings](tutorials/basics/40_strings.md)
* [Control Flow](tutorials/basics/50_control_flow.md)
* [Methods](tutorials/basics/60_methods.md)
* [Blocks](tutorials/basics/70_blocks.md)
* [Manuals](man/README.md)
* [Using the Compiler](man/crystal/README.md)
* [The Shards Command](man/shards/README.md)
29 changes: 29 additions & 0 deletions docs/tutorials/basics/60_methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,32 @@ puts life_universe_and_everything + 1 # Error: method top-level life_universe_an
```

Now the compiler can show us exactly where the problem is originated. As we can see, providing type information is really useful for finding errors at compile time.

## Blocks

While working with *collections* (just as an example) we may have came across code that looks like this:

```crystal-play
["Hello", "World", 42].each do |elem|
puts elem
end
```

or maybe something like this:

```crystal-play
arr = [1, 2, 3].map do |elem|
elem * 2
end

puts arr
```

In the first example we are using the method [Indexable#each](https://crystal-lang.org/api/Indexable.html#each%28%26%3AT-%3E%29-instance-method).
And in the second example we are using the method [Enumerable#map](https://crystal-lang.org/api/Enumerable.html#map%28%26%3AT-%3EU%29%3AArray%28U%29forallU-instance-method).

To both methods we are passing a *block of code* (delimited by the keywords `do` and `end`) as an argument.

This *block of code*, commonly called *block* is a sequence of interactions, which will be executed at some point. When exactly? It depends on the method that receives the *block* as a parameter (in these examples `each` and `map`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Italics are good for the first occurrence of a topic. Subsequent occurrences should not be emphasized (in a section).

Suggested change
This *block of code*, commonly called *block* is a sequence of interactions, which will be executed at some point. When exactly? It depends on the method that receives the *block* as a parameter (in these examples `each` and `map`).
This block of code, commonly called *block* is a sequence of interactions, which will be executed at some point. When exactly? It depends on the method that receives the block as a parameter (in these examples `each` and `map`).


*Blocks* is a really interesting and important topic, so it has its own section. Let's continue [there](70_blocks.md)!
Loading