Crystal Short Block Syntax

Is it possible to use shorthand for blocks in Crystal, for example.

my_array.sort_by(&:size)

This attempt returns an error:

... expected function type, not character

+4
source share
1 answer

You can use this syntax:

my_array = ["123", "22", "1"]
sorted = my_array.sort_by &.size
puts sorted
=> ["1", "22", "123"]
+4
source

Source: https://habr.com/ru/post/1629881/


All Articles