Structuring Data with Container Types
Simple variables provide a solid foundation, but as your programs grow, you won’t want to keep juggling hundreds of variable names in your head. Crystal provides a number of collection types that let you store a lot of information in structured ways. Arrays and hashes will get you started, and tuples and sets will appear later in the book.
Using Arrays
Sometimes you need to create a list of values, kept in order, and accessible by position. Because this is Crystal, let’s collect some minerals. (All of these mineral names are real, and there are lots of them!). The simplest way is to use an array:
| minerals = ["alunite", "chromium", "vlasovite"] |
| typeof(minerals) ... |
Get Programming Crystal now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.