Chapter 7. Hashes: Labeling Data
Throwing things in piles is fine, until you need to find something again. You’ve already seen how to create a collection of objects using an array. You’ve seen how to process each item in an array, and how to find items you want. In both cases, you start at the beginning of the array, and look through Every. Single. Object. You’ve also seen methods that take big collections of parameters. You’ve seen the problems this causes: method calls require a big, confusing collection of arguments that you have to remember the exact order for.
What if there were a kind of collection where all the data had labels on it? You could quickly find the elements you needed! In this chapter, we’ll learn about Ruby hashes, which do just that.
Counting votes
A seat on the Sleepy Creek County School Board is up for grabs this year, and polls have been showing that the election is really close. Now that it’s election night, the candidates are excitedly watching the votes roll in.
{"name" => "Amber Graham", "occupation" => "Manager"}
{"name" => "Brian Martin", "occupation" => "Accountant"}
The electronic voting machines in use this year record the votes to text files, one vote per line. ...
Get Head First Ruby 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.