Exercise 8 of 10 · Arrays
Packing List
What you will make
A travel packing checklist with a tick-box line for every item on the list, and a footer that counts them for you.
The one new idea: An array holds several values in order, and each walks through them
Real programs almost never hold one value at a time. They hold a basket of them — orders, players, songs — and do the same thing to each one. An array paired with each is how Ruby does that, and it is a pairing you will reach for constantly.
Go straight to the code ↓An array is many values under one name
Every variable so far has held exactly one thing. A packing list is not like that — it is one thing made of many things, kept in the order you wrote them down. Ruby writes that with square brackets and a comma between each value:
items = ["Passport", "Charger"]That is a single variable, items, holding two pieces of text in order. Adding a third means typing a comma and another value inside the same brackets.
downto and the range-based loop from earlier both count numbers. each, called directly on an array, does something different: it walks along the array and hands you the value sitting at each position, one at a time, in the block variable.
A worked example
colors = ["Red", "Blue", "Green"]
colors.each do |color|
puts "Mixing in #{color}"
end
puts "#{colors.length} colors used"Mixing in Red
Mixing in Blue
Mixing in Green
3 colors usedThree values on the array, three lines of output, then a line that counted them with .length. The name between the | bars is yours to choose — color, item, whatever reads best — and Ruby fills it in fresh on every pass.
Your turn
The editor holds a checklist with two items on it. Press Run first: the loop is already doing its job, running twice, once per item — but both lines read [ ] item, because the line inside the loop prints the literal word item rather than the value the block variable is holding.
Two things to do:
- Rewrite the line inside the loop so it interpolates
item, the way the worked example interpolatescolor. - Add
"Socks"and"Towel"to the array, in that order, inside the square brackets.
The footer needs no attention. It already counts the array with .length, so it will read 4 on its own the moment the two new items are in place.
If something goes wrong
If every line still reads [ ] item word for word, the #{...} around it is missing. If a line reads [ ] ChargerSocks and the footer is one short, a comma went missing between two items in the array — Ruby joins touching string literals into one instead of raising an error, so the checklist itself is the only warning you get.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Looping with for item in items instead of items.each
- It still runs and still works, but a for loop's variable stays alive after the loop ends, quietly hanging around in the rest of the program. each's block variable exists only inside the do ... end, which is why Ruby style leans on each almost everywhere.
- Leaving out the # before the braces
- Without it, every line prints [ ] item literally, word for word, on every pass — there is no error, because a string with the word item typed inside it is perfectly legal text.
- Forgetting the comma between two items in the array
- "Charger" "Socks" sitting next to each other with no comma between them does not raise an error — Ruby quietly joins adjacent string literals into one, so the checklist shows a single line reading ChargerSocks and the footer counts one item fewer than expected.
- Typing the new count into the footer by hand
- The footer already works out its own number from items.length, so it updates itself the moment the list changes. Replacing it with a typed number means the line stops telling the truth the next time an item is added or removed.
Want a blank editor instead? Open the Ruby playground.