Кортеж
Кортеж обычно создаётся с помощью литерала кортежа:
tuple = {1, "hello", 'x'} # Tuple(Int32, String, Char)
tuple[0] # => 1 (Int32)
tuple[1] # => "hello" (String)
tuple[2] # => 'x' (Char)
Чтобы создать пустой кортеж, используйте Tuple.new.
Чтобы указать тип кортежа, можно написать:
# The type denoting a tuple of Int32, String and Char
Tuple(Int32, String, Char)
В ограничениях типов, аргументах обобщённых типов и других местах, где ожидается тип, можно использовать более короткий синтаксис, описанный в грамматике типов:
# An array of tuples of Int32, String and Char
Array({Int32, String, Char})
Распаковка с помощью оператора splat
Оператор splat можно использовать внутри литералов кортежей, чтобы распаковать несколько значений одновременно. Распаковываемое значение должно быть другим кортежем.
tuple = {1, *{"hello", 'x'}, 2} # => {1, "hello", 'x', 2}
typeof(tuple) # => Tuple(Int32, String, Char, Int32)
tuple = {3.5, true}
tuple = {*tuple, *tuple} # => {3.5, true, 3.5, true}
typeof(tuple) # => Tuple(Float64, Bool, Float64, Bool)
To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/reference/1.2/syntax_and_semantics/literals/tuple.html