ruby · level 1 · unit 6 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An keeps many values together, in order.
An array is like a shopping list: many values kept together, in order, in one variable. Write it with square brackets, and put a comma between each .
Each value in an array is an item. Items can be strings, numbers or a mix. .length tells you how many items there are, the same way it counts letters in a string.
To see a whole array, use . It shows the array the way you would write it, brackets and quotes included. puts shows each item on a line of its own instead.
snacks = ["crisps", "apple", "popcorn"]
p snacks
puts snacks.length
puts snacks An empty array, [], is a list with nothing on it yet.
guess first
puts meets an array. What will it show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
pets = ["cat", "dog", "fish"]
puts pets
your turn
Your turn. Pick your dream team.
Type three names for your dream team, one in each pair of quotes. Then press run to see your array and how many players it holds.
fix it
Two snacks got squashed together. Can you fix it?
- Run it. How many items does the array have? Is that what you wanted?
- Two strings with no comma between them get squashed into one item.
- Put a comma after "crisps", then press run.
nailed it
you made your first array. :D
- An array keeps items together, in order, in square brackets.
- Put a comma between each item.
pshows the whole array.putsshows one item per line.
new words in your collection
- array
- item
- p
This lesson isn’t saved. A free account remembers every lesson you finish.