Tuple is a data structure which has immutable elements and is of a fixed size. Its similar to an array, but unlike arrays in crystal and ruby which allows adding more values over time a tuple is of fixed and cannot change. (Disclaimer: This article is meant for Ruby developer and explaining what a tuple is to a ruby developer).

In crystal we have two types of tuple

1) Tuple {1, "hello", :world}
2) NamedTuple {x: 1, y:2, z: 5}

They are immutable, which means if you try changing the value of an element in a tuple you will get an exception. Since crystal programs are compiled before execution you will get to see these errors while you compile the program itself.

example:

If you try assigning a value to it, like so

You will get an exception like bellow.

In crsytal, tuples are the preferred way to return a multiple results from a method.

For example inside the crystal core we have a method to get the minimum and maximum of an array.

the result would be {1, 100}

Note: Since we just mentioned minmax, have a look at minmax_by method as well. It would let you apply a block of code over your range and then return the minimum and maximum based on the returned collection.

Advantage of using tuple to return results instead of something like hash, is that we can be sure that our result cannot be altered accidentally. (since the data structure is immutable) 😁

You can build a tuple from an array by using the .from method

As a developer, the place where we use tuple the most in crystal are with splats(symbol: *). Passing arguments to method using splat and double splat operator is something we use widely in ruby keep our code small and readable. So if you wish to do the same in crystal you need to make a tuple not a hash or array.

If you use splat on an array directly like test(*[1,2]) it would return an error

So to achieve the same effect as a splat with array in crystal we would need to do test(*{1,2})

Named Tuple

Named Tuple are everything as above, but with a name for each element. Named Tuple looks like {x: 1, y:2} it gives more meaning to our tuple. Like the above you can access the values but not change them.

Double splats are meant for Named Tuple where in we can pass in the values for a particular argument using named tuple and double splat.

You can build a NamedTuple from a hash.

Note: Crystal has a nifty feature called Union types (a variable can store data of multiple data types), so if it happen to pass such a variable to a named tuple/tuple, it will still check for the exact type that we want if the data is not in that variable then an exception would be raised

Example

Exception:

Extra Note:

If you put a splat before method argument and pass in arguments, they will be converted to a tuple

If you put a double splat before method argument and pass in data as keyword argument it gets converted to a NamedTuple

 

To summarize:

  • Tuples are immutable data structure
  • Regular tuple is like a frozen array
  • You can use splat only with a tuple
  • NamedTuple is like a frozen hash
  • Double splat can only be used with NamedTuple