Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this video, we're going to explore some of the methods that Ruby provides us for working with hashes.
Links
Code Samples
For the examples below, we'll be working with the following hash:
hash = { "item" => "Bread", "quantity" => 1, "brand" => "Treehouse Bread Company" }
The #length
method will return the number of keys in the hash. In this case, it would be 3:
hash.length
The #invert
method returns a new hash with the keys and values transposed:
hash.invert
That would produce the following new hash:
{"Bread" => "item", 1 => "quantity", "Treehouse Bread Company" => "brand"}
The #shift
method works similar to hashes as it does with arrays. It will remove a key and value pair from the hash and return it as an array:
hash.shift
This would return the following (note that it is an array):
["item", "Bread"]
The original hash would also be modified:
{"quantity" => 1, "brand" => "Treehouse Bread Company"}
The #merge
method combines the hash sent in as an argument and returns a new hash with the two combined:
hash.merge({"calories" => 100})
Would return the following:
{"quantity" => 1, "brand" => "Treehouse Bread Company", "calories" => 100}
If any key value pairs exist in the original hash, the merge
method will overwrite those:
hash.merge({"quantity" => 100})
Would return:
{"quantity" => 100, "brand" => "Treehouse Bread Company"}
-
0:01
Just like strings and
-
0:02
numbers, Ruby gives us a lot of methods we can use to work with hashes.
-
0:08
These methods can operate on keys and values at the same time.
-
0:13
Let's take a look at how some of them work now, using work spaces.
-
0:17
Okay, now that we know how to create and work with hashes, let's go ahead and
-
0:21
start learning about some different hash methods.
-
0:24
And I'm gonna create a file for this and we're gonna call it hashmethods.rb.
-
0:30
Now let's go ahead and set up a hash to work with.
-
0:35
And we'll have the keys of hash, item, and that's bread.
-
0:43
And we only want one.
-
0:48
And the brand is Treehouse Bread Company.
-
0:52
Okay, now, let's go ahead and just print out the hash.
-
1:00
Let's just run this and make sure everything is working and nothing's crazy.
-
1:05
So click down into the console, and type ruby and then hash_methods.rb,
-
1:10
and there is a printout of our hash.
-
1:15
Now I'm gonna get these hash methods from the Ruby Hash documentation.
-
1:20
Some of the methods that we're going to look for here, the first one is length.
-
1:26
And that will return the number of key value pairs in the hash.
-
1:32
Let's go ahead and see how that works now.
-
1:35
And we'll just say hash dot length.
-
1:39
And print it out.
-
1:43
And let's run this again and see what happens.
-
1:47
All right, the hash has a length of three.
-
1:50
And you know what let's change that to print.
-
1:54
So it's all on the same line.
-
1:56
Clearing my screen on the bottom there with Ctrl+L, and there we go,
-
2:01
we have a length of 3.
-
2:02
Another neat method is something called invert and
-
2:07
that will transpose the keys and values into a new hash.
-
2:14
And let's go ahead and print that to the screen as well.
-
2:19
I'm gonna save that and run that again and now if we look, here's our original hash.
-
2:27
But when we run the invert method, it changes all the keys and values.
-
2:32
And it does not change the original hash.
-
2:38
The next method that we can take a look at is called shift.
-
2:43
Now if I click on the methods list here in the hash documentation.
-
2:47
This will remove a key value pair from the hash and
-
2:51
return it as a two item array with the key and value.
-
2:56
Let's go back to our workspace and see how that works.
-
3:05
So we're gonna print out the hash after shifting it as well.
-
3:14
And we can use the inspect method to print out the hash.
-
3:18
Let me clear out my screen on the bottom again, with Ctrl + L and run this again.
-
3:24
Now, we've called hash.shift and this returns item and
-
3:28
bread in an array form, but are we really sure it's an array?
-
3:33
Let's go ahead and call the inspect method on that to make sure.
-
3:38
So when we run that again, we can see this comes back as an array,
-
3:44
and our hash now does not have the item Item is the key.
-
3:49
Let's go ahead and put the item back in the hash.
-
3:52
So we'll say item, hash, item is bread.
-
3:58
Now let's go back and look at some more methods.
-
4:01
The last method that we're gonna look at is called merge.
-
4:04
Now merge gives an argument of another hash and returns a new hash.
-
4:10
That sounds a little confusing, so let's go ahead and see how it works.
-
4:15
And we can just print this out now.
-
4:21
So here's our hash, let's print that out.
-
4:27
And now, let's print this out and merge it with a couple other hashes.
-
4:38
Now we call the merge method and then we pass in another hash.
-
4:49
And then we'll go ahead and
-
4:50
print the original hash out again, just to see what it looks like.
-
4:55
Clear my screen since this is getting to be a lot of output and we'll run it again.
-
5:01
So now we've printed out Merged with this particular hash and
-
5:07
what we get back is a new hash.
-
5:10
You know what, that's a little hard to read.
-
5:11
Let's change this to puts and run it again.
-
5:17
So here's our original hash.
-
5:19
Now we merge it with calories of 100 and we get back this hash,
-
5:25
which contains another hash with calories of 100 added on to the original hash.
-
5:34
But, if we look back at that original hash, it does not have the calories key.
-
5:39
This is because merge will create a new hash.
-
5:45
Now you might wonder what would happen,
-
5:47
if we called merge with a key that already exists inside the hash.
-
5:52
Let's go ahead and see what happens.
-
5:59
And we'll just merge that with a different item name.
-
6:12
Now let's clear the screen here and run it again and see what happens.
-
6:16
Now when we do it, we'll see that the new hash comes back
-
6:21
with only one key of item which points to eggs.
-
6:26
Which is the last thing that we called in with merge.
-
6:30
When merge has a conflicting hash key, the hash that's sent into merge
-
6:36
receives precedence and will overwrite the existing key value pair.
-
6:41
Using the documentation linked in the teacher notes section of this video, try
-
6:46
looking up and practicing with different hash methods on your own using workspaces.
You need to sign up for Treehouse in order to download course files.
Sign up