1 00:00:01,130 --> 00:00:02,480 Just like strings and 2 00:00:02,480 --> 00:00:08,060 numbers, Ruby gives us a lot of methods we can use to work with hashes. 3 00:00:08,060 --> 00:00:12,200 These methods can operate on keys and values at the same time. 4 00:00:13,360 --> 00:00:17,379 Let's take a look at how some of them work now, using work spaces. 5 00:00:17,379 --> 00:00:21,391 Okay, now that we know how to create and work with hashes, let's go ahead and 6 00:00:21,391 --> 00:00:24,130 start learning about some different hash methods. 7 00:00:24,130 --> 00:00:30,960 And I'm gonna create a file for this and we're gonna call it hash_methods.rb. 8 00:00:30,960 --> 00:00:33,730 Now let's go ahead and set up a hash to work with. 9 00:00:35,620 --> 00:00:39,879 And we'll have the keys of hash, "item", and that's "bread". 10 00:00:43,088 --> 00:00:44,390 And we only want one. 11 00:00:48,212 --> 00:00:50,590 And the brand is "Treehouse Bread Company". 12 00:00:52,610 --> 00:00:56,713 Okay, now, let's go ahead and just print out the hash. 13 00:01:00,629 --> 00:01:05,340 Let's just run this and make sure everything is working and nothing's crazy. 14 00:01:05,340 --> 00:01:10,454 So click down into the console, and type ruby and then hash_methods.rb, 15 00:01:10,454 --> 00:01:13,920 and there is a printout of our hash. 16 00:01:15,120 --> 00:01:20,840 Now I'm gonna get these hash methods from the Ruby Hash documentation. 17 00:01:20,840 --> 00:01:26,650 Some of the methods that we're going to look for here, the first one is length. 18 00:01:26,650 --> 00:01:30,950 And that will return the number of key value pairs in the hash. 19 00:01:32,010 --> 00:01:33,560 Let's go ahead and see how that works now. 20 00:01:35,130 --> 00:01:36,800 And we'll just say hash dot length. 21 00:01:39,300 --> 00:01:40,190 And print it out. 22 00:01:43,360 --> 00:01:46,700 And let's run this again and see what happens. 23 00:01:47,922 --> 00:01:50,610 All right, the hash has a length of three. 24 00:01:50,610 --> 00:01:52,830 And you know what let's change that to print. 25 00:01:54,350 --> 00:01:55,410 So it's all on the same line. 26 00:01:56,570 --> 00:02:01,334 Clearing my screen on the bottom there with Ctrl+L, and there we go, 27 00:02:01,334 --> 00:02:02,879 we have a length of 3. 28 00:02:02,879 --> 00:02:07,035 Another neat method is something called invert and 29 00:02:07,035 --> 00:02:11,585 that will transpose the keys and values into a new hash. 30 00:02:14,962 --> 00:02:19,087 And let's go ahead and print that to the screen as well. 31 00:02:19,087 --> 00:02:25,685 I'm gonna save that and run that again and now if we look, here's our original hash. 32 00:02:27,160 --> 00:02:32,171 But when we run the invert method, it changes all the keys and values. 33 00:02:32,171 --> 00:02:35,087 And it does not change the original hash. 34 00:02:38,129 --> 00:02:42,420 The next method that we can take a look at is called shift. 35 00:02:43,730 --> 00:02:47,337 Now if I click on the methods list here in the hash documentation. 36 00:02:47,337 --> 00:02:51,344 This will remove a key value pair from the hash and 37 00:02:51,344 --> 00:02:55,560 return it as a two item array with the key and value. 38 00:02:56,930 --> 00:02:59,461 Let's go back to our workspace and see how that works. 39 00:03:05,337 --> 00:03:09,378 So we're gonna print out the hash after shifting it as well. 40 00:03:14,060 --> 00:03:18,005 And we can use the inspect method to print out the hash. 41 00:03:18,005 --> 00:03:24,440 Let me clear out my screen on the bottom again, with Ctrl + L and run this again. 42 00:03:24,440 --> 00:03:28,514 Now, we've called hash.shift and this returns "item" and 43 00:03:28,514 --> 00:03:33,530 "Bread" in an array form, but are we really sure it's an array? 44 00:03:33,530 --> 00:03:36,100 Let's go ahead and call the inspect method on that to make sure. 45 00:03:38,740 --> 00:03:44,812 So when we run that again, we can see this comes back as an array, 46 00:03:44,812 --> 00:03:49,880 and our hash now does not have the item "item" as a key. 47 00:03:49,880 --> 00:03:52,421 Let's go ahead and put the item back in the hash. 48 00:03:52,421 --> 00:03:56,370 So we'll say item hash["item"] is "bread". 49 00:03:58,790 --> 00:04:01,310 Now let's go back and look at some more methods. 50 00:04:01,310 --> 00:04:04,300 The last method that we're gonna look at is called merge. 51 00:04:04,300 --> 00:04:10,510 Now merge gives an argument of another hash and returns a new hash. 52 00:04:10,510 --> 00:04:13,922 That sounds a little confusing, so let's go ahead and see how it works. 53 00:04:15,045 --> 00:04:16,255 And we can just print this out now. 54 00:04:21,296 --> 00:04:24,837 So here's our hash, let's print that out. 55 00:04:27,379 --> 00:04:31,587 And now, let's print this out and merge it with a couple other hashes. 56 00:04:38,171 --> 00:04:42,140 Now we call the merge method and then we pass in another hash. 57 00:04:49,280 --> 00:04:50,170 And then we'll go ahead and 58 00:04:50,170 --> 00:04:53,890 print the original hash out again, just to see what it looks like. 59 00:04:55,720 --> 00:05:00,240 Clear my screen since this is getting to be a lot of output and we'll run it again. 60 00:05:01,660 --> 00:05:07,432 So now we've printed out Merged with this particular hash and 61 00:05:07,432 --> 00:05:10,212 what we get back is a new hash. 62 00:05:10,212 --> 00:05:11,600 You know what, that's a little hard to read. 63 00:05:11,600 --> 00:05:14,650 Let's change this to puts and run it again. 64 00:05:17,230 --> 00:05:19,360 So here's our original hash. 65 00:05:19,360 --> 00:05:25,680 Now we merge it with calories of 100 and we get back this hash, 66 00:05:25,680 --> 00:05:34,080 which contains another hash with calories of 100 added on to the original hash. 67 00:05:34,080 --> 00:05:39,460 But, if we look back at that original hash, it does not have the calories key. 68 00:05:39,460 --> 00:05:42,940 This is because merge will create a new hash. 69 00:05:45,170 --> 00:05:47,270 Now you might wonder what would happen, 70 00:05:47,270 --> 00:05:52,990 if we called merge with a key that already exists inside the hash. 71 00:05:52,990 --> 00:05:56,171 Let's go ahead and see what happens. 72 00:05:59,690 --> 00:06:02,297 And we'll just merge that with a different item name. 73 00:06:12,046 --> 00:06:15,769 Now let's clear the screen here and run it again and see what happens. 74 00:06:16,920 --> 00:06:21,750 Now when we do it, we'll see that the new hash comes back 75 00:06:21,750 --> 00:06:26,890 with only one key of "item" which points to "Eggs". 76 00:06:26,890 --> 00:06:29,740 Which is the last thing that we called in with merge. 77 00:06:30,900 --> 00:06:36,090 When merge has a conflicting hash key, the hash that's sent into merge 78 00:06:36,090 --> 00:06:40,300 receives precedence and will overwrite the existing key value pair. 79 00:06:41,660 --> 00:06:46,390 Using the documentation linked in the teacher's notes section of this video, try 80 00:06:46,390 --> 00:06:51,680 looking up and practicing with different hash methods on your own using workspaces.