January 2019
Intermediate to advanced
246 pages
5h 23m
English
Composite types, like the arrays and hashes we met in the previous chapter, make it easier to create more complex applications. Crystal offers more options, like tuples and sets, to address other data models.
Tuples group related values of possibly different types. You can create them with values inside { }, or with Tuple.new:
| | tpl = {42, "silver", 'C'} |
| | tpl.class # => Tuple(Int32, String, Char) |
| | a = Tuple.new(42, "silver", 'C') |
In case you’re wondering how multiple assignments, such as n, m = 42, 43, work, they use tuples. You can access items of a tuple by index:
| | tpl[0] # => 42 (Int32) |
| | tpl[1] # => "silver" (String) |
| | tpl[2] # => 'C' (Char) |
| | var = 89 |
| | tpl[var] # => Index ... |
Read now
Unlock full access