Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand block syntax in Crystal

Tags:

crystal-lang

Is it possible to use a shorthand for blocks in Crystal, e.g.

my_array.sort_by(&:size)

This attempt returns an error:

... expected a function type, not Symbol

like image 223
Charlie Egan Avatar asked Feb 22 '16 11:02

Charlie Egan


1 Answers

You can use this syntax:

my_array = ["123", "22", "1"]
sorted = my_array.sort_by &.size
puts sorted
=> ["1", "22", "123"]
like image 108
dimid Avatar answered Sep 19 '22 04:09

dimid