Home
Username:
Password:
Programming With Ruby Tutorials

Arrays/Hashes / More Hash Manipulation




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

In this video we're going to look at a few more methods that we can use with a hash. So the first thing I wanted to look at here was merging two hashes. So I've already got IRB running in the window here and I've instantiated two hashes, one X, one called color and I'm just going to merge those together and I'll call it a new hash and so what we do is we call, take one of our hashes, we call the method merge and then we give it the other hash inside the parentheses as the argument and so now we have a hash that combines the two. Uh, this is a bit of a contrived example, but say you wanted to combine two dictionaries together or something along those lines, you have the option to do that and so it's a pretty straightforward method to utilize in putting two together. Now, say we have an array. Uh, for example, let's do just a simple array like this. And I could, we want to convert that to a hash. Well, let's see what happens if we just do Y hash equal. Now, the calling structure is a, it's a touch odd in what you have to do as far as calling it because it has this, this asterisk that you're going to use to actually put it in there. We're going to do this. Now, it's not going to be happy. And the reason it's not happy is it's telling here odd number of arguments. It doesn't like, you know, a hash doesn't like odd things because it needs a key and a value and in this case, how this works is this is going to be your, your key and this is your values. So let's re-declare Y and add a six on there so we have an even number. Now let's call this and sure enough, we're happy now and look at that ordering. You see how that ordering changed and it's not one, two, three, four. Instead we, we're going five, one, three. So ordering got totally messed up. Be careful with that. But at least it's a hash. But what it's done is taken these as pairs. So the first element is the key, the second element is the value. You see what it's done here. Now, we can go the other way, too and you've already seen that in the sorting video that we did on hash, is that you can go from a hash to an array. So if we take our color hash, which is right there, and we say two A, which stands for two array, now we have an array. But each element of that array is a two-element array. You see, right here, that is an array with two elements inside of this array, the bigger one, that has the three elements. So that's a little bit more on manipulating hashes.

Tutorial Information

Course: Programming With Ruby
Author: Al Anderson
SKU: 33788
ISBN: 1-934743-01-1
Release Date: 2007-08-21
Duration: 8.5 hrs / 113 lessons
Work Files: Yes
Captions: For Online University members only
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available