January 2019
Intermediate to advanced
246 pages
5h 23m
English
I know you like beautiful code—otherwise you wouldn’t be reading this book. Data conversion often requires a predictable sequence of methods. One of the nice things about Crystal is that you can chain methods to create more readable code. A method can be called on any object on which it’s defined, and that object can be the result of another method call. Take a look at the following examples:
| ① | result = (42..47).to_a # => [42, 43, 44, 45, 46, 47] |
| ② | .sort { |n, m| m <=> n } # => [47, 46, 45, 44, 43, 42] |
| ③ | .reject { |n| n.odd? } # => [46, 44, 42] |
| ④ | .map { |n| n * n } # => [2116, 1936, 1764] |
| ⑤ | .select { |n| n % 4 == 0 } # => [2116, 1936, 1764] |
| ⑥ | .tap { |arr| puts ... |
Read now
Unlock full access