Locked lesson.
About this lesson
Adding and removing items from a hash.
Exercise files
Download this lesson’s related exercise files.
Hash Manipulation.docx59 KB Hash Manipulation - Solution.docx
59.3 KB
Quick reference
Hash Manipulation
You can add and remove items to your hash later on programmatically.
When to use
Use this any time you want to add or remove something from your hash.
Instructions
To add an item to your hash:
favorite_pizza = {
"John" => "Pepperoni",
"Tim" => "Mushroom",
"Mary" => "Cheese"
}
favorite_pizza["Steve"] = "Mushroom"
To remove an item from your hash:
favorite_pizza.delete("Steve")
Hints & tips
- It's easy to add or remove an item from your hash programmatically
- 00:04 We've got our hash, we've created it.
- 00:06 What if we want to add something later on, or remove something later on, to our hash?
- 00:10 That's what we're going to talk about in this video.
- 00:12 But before we do that, I just want to really quickly, let's puts out our
- 00:15 favorite _pizza, just puts this on the screen and see what happens.
- 00:18 Slightly different than our array, we get the actual sort of
- 00:21 objecty-looking thing itself, which is kind of interesting.
- 00:24 How do we manipulate our hash?
- 00:25 So let's say we want to add something to our hash later on.
- 00:29 We've already created it, it already exists.
- 00:31 Now, later on, we're like, I forgot something,
- 00:33 we want to put something else in, how do we do that?
- 00:35 Well, it's pretty simple, we just go favorite_pizza and then type in the key.
- 00:40 So let's say we want to add, who don't we have, Steve.
- 00:44 And Steve's favorite_pizza = "Hamburger", right?
- 00:49 So now we can puts our favorite_pizza to the screen just to see if it got added.
- 00:55 In fact, let's copy this, and let's put it before and after, so we can see.
- 01:02 So let's run this thing, you can see the first time we puts it out,
- 01:05 there's no mention of Steve.
- 01:07 But then we've added Steve here, and then, boom, there he is.
- 01:10 So pretty simple to do, that's all there is to it.
- 01:13 Now removing somebody is just as easy, we just use the delete method.
- 01:17 So let's come up here and copy this, and
- 01:20 let's say we don't want Steve in there after all.
- 01:24 So we just go call favorite_pizza again and then .delete.
- 01:30 And like I said before in the past, a lot of times when we want to do things in
- 01:34 Ruby, we just put a dot after the thing, that's an object-oriented type thing.
- 01:38 So this time, instead of the brackets, we're going to do these parentheses, so
- 01:43 let's go Steve.
- 01:44 Okay, so let's puts out our hash more time afterwards and
- 01:47 make sure Steve has been deleted.
- 01:49 So let's run this thing, so the first time, Steve's not there,
- 01:52 then we've added Steve, so he's listed.
- 01:54 Then we remove Steve, and then finally Steve is gone.
- 01:57 So that's how you add a thing to a hash,
- 01:59 that's how you delete a thing from a hash, very, very simple.
- 02:02 So if we want to o do this programmatically, we could go,
- 02:07 let's go print "Enter Your Name: And
- 02:11 then let's call my_name = gets.chomp,
- 02:17 and then print "Enter Pizza", and
- 02:22 then pizza = gets.chomp.
- 02:26 Okay, so now we can go favorite_pizza,
- 02:32 and pass in our my_name = pizza.
- 02:37 And now that that will add the user stuff we just gets-ed.
- 02:41 And then let's just puts out our favorite_pizza.
- 02:45 And in fact, I'm going to copy this, and let's put it up before as well, so
- 02:49 we can see the before and after.
- 02:51 So now if we run this, here's our original hash.
- 02:55 And let's enter Terry, and pizza, Terry likes tuna pizza, is that even a thing?
- 03:01 So then when our hash gets printed out again, now we see Terry likes tuna.
- 03:06 I can put a colon and a space there to make it nice.
- 03:10 But anyway, it's just that easy to add and remove things from a hash.
- 03:13 You can do it programmatically or you can do it with user input, however you'd like.
- 03:17 So that's hashes, that's hash manipulation, in the next video,
- 03:20 we're going to start looking at methods.
Lesson notes are only available for subscribers.