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
- 00:04 In this video, I want to talk about hashes and hashes are an awful lot like arrays.
- 00:08 In fact, let's go ahead and create an array again.
- 00:12 And let's just do our basic John, Tim, Mary,
- 00:18 Bluto, whatever, and we remember to access this
- 00:24 we need to say puts names and then give it an index number.
- 00:29 So if we want Tim, we would put 1, so if we save this and run it, we get Tim.
- 00:35 The problem with arrays is you have to know the numbers of the things.
- 00:39 So in order to output Tim, I need to know that it's the 1th item in our array.
- 00:44 If I want to print out John, I need to know that it's the 0th item in our array.
- 00:48 And that can be kind of a hassle.
- 00:50 Well, hashes, like I said,
- 00:51 they're very much like arrays only we don't need to know the numbers.
- 00:54 They're not indexed by numbers, they're indexed by keywords.
- 00:57 So to create a hash, start out the same, we name it.
- 01:01 So I'm going to name this favorite_pizza and set it equal to and an array
- 01:06 we use those straight brackets, with a hash we use our curly brackets, right.
- 01:12 So hashes have pairs.
- 01:14 They're called keys and values, and the key is on the left and
- 01:18 the value is on the right.
- 01:19 They're separated by something called a hash rocket.
- 01:21 So to build one of these things we just, let's go,
- 01:23 John, and then here's our hash rocket.
- 01:26 It kind of looks like a little rocket, an arrow pointing that way.
- 01:29 And then we add the value, the thing we want to store.
- 01:31 So my favorite pizza is pepperoni.
- 01:35 Is that how you spell pepperoni?
- 01:36 I have no idea, pepperoni, and like an array, we separate each item with a comma.
- 01:40 You can put all these on the same line like an array but
- 01:43 it quickly becomes very hard to read.
- 01:45 So the convention is to put each item on its own separate line.
- 01:48 So we could go Tim and hash rocket him, Tim likes mushroom and
- 01:55 we've got Mary, Mary likes cheese.
- 02:02 And we've got Bluto and
- 02:05 Bluto likes supreme pizza.
- 02:09 So Bluto is the last guy, so he doesn't need a comma at the end.
- 02:13 So we save this.
- 02:14 Now we've got our hash.
- 02:15 So how do we access a hash?
- 02:17 It's very much like an array.
- 02:19 We go puts, and then call it favorite_pizza.
- 02:22 And then just like an array, we use our brackets.
- 02:25 And now you call the key, so like I said, the key's on the left,
- 02:28 the value is on the right.
- 02:29 So if we call the key of John, it will output John's value,
- 02:33 John's favorite pizza, in this case, pepperoni.
- 02:36 So if we save this, I'll go up here and
- 02:38 hit reload, we're putting out let's comment this guy out.
- 02:42 We save it, hit reload, we get pepperoni.
- 02:45 So if we want to know Mary's favorite pizza,
- 02:48 we just type in Mary and we get cheese.
- 02:51 If we want to know Bluto, just type in Bluto, supreme.
- 02:56 Now the problem with arrays, you have to know which number is the item.
- 03:00 So if we want to know Tim's number, we need to know he's the 1th person.
- 03:04 Hashes you don't need to do that.
- 03:05 You just need to type in Tim and you'll get Tim's value.
- 03:08 Now of course, you still need to know that there's a Tim in there.
- 03:11 But now what do you want?
- 03:13 So those are hashes, very powerful.
- 03:15 You'll use hashes an awful lot.
- 03:17 Sometimes it's a matter of using an array or a hash,
- 03:20 and just depending on the data that you have,
- 03:22 the stuff that goes in it will determine whether you use a hash or an array.
- 03:26 They act very similar.
- 03:27 You can put all kinds of stuff in hashes.
- 03:29 You can put hashes inside of an array, right?
- 03:33 So we put arrays in our arrays, we put strings in our arrays, we put numbers,
- 03:37 variables, you can put hashes.
- 03:39 So in the next video, we'll look at how to manipulate hashes.
Lesson notes are only available for subscribers.