Tuple

A Tuple is typically created with a tuple literal:

  1. tuple = {1, "hello", 'x'} # Tuple(Int32, String, Char)
  2. tuple[0] # => 1 (Int32)
  3. tuple[1] # => "hello" (String)
  4. tuple[2] # => 'x' (Char)

To create an empty tuple use Tuple.new.

To denote a tuple type you can write:

  1. # The type denoting a tuple of Int32, String and Char
  2. Tuple(Int32, String, Char)

In type restrictions, generic type arguments and other places where a type is expected, you can use a shorter syntax, as explained in the type grammar:

  1. # An array of tuples of Int32, String and Char
  2. Array({Int32, String, Char})

Splat Expansion

The splat operator can be used inside tuple literals to unpack multiple values at once. The splatted value must be another tuple.

  1. tuple = {1, *{"hello", 'x'}, 2} # => {1, "hello", 'x', 2}
  2. typeof(tuple) # => Tuple(Int32, String, Char, Int32)
  3. tuple = {3.5, true}
  4. tuple = {*tuple, *tuple} # => {3.5, true, 3.5, true}
  5. typeof(tuple) # => Tuple(Float64, Bool, Float64, Bool)