Locked lesson.
About this lesson
What is a hash, and how do you create and use it?
Exercise files
Download this lesson’s related exercise files.
Hashes.docx60.2 KB Hashes - Solution.docx
59.2 KB
Quick reference
Hashes
Let's learn about hashes.
When to use
Hashes are similar to arrays, but instead of using numbers as indexes, they use key value pairs.
Instructions
Here's how to build a basic hash:
favorite_pizza = {
"John" => "Pepperoni",
"Tim" => "Mushroom",
"Mary" => "Cheese"
}
To call an item from the Hash:
puts favorite_pizza["John"]
Hints & tips
- Hashes are like arrays
- Instead of index numbers, they use key value pairs
Lesson notes are only available for subscribers.