



(2 ratings)
Hashes are a generalization of arrays. Instead of only permitting integer indices, as in array[3], hashes allow any object to be used as an "index". So, you can write hash["name"]
Suppose that I want to store some information about a friend. I could use an array, like this:
friend = [ "Jeffrey", "Biggs", "34 Airport Rd", |
This could work, but I would have to remember that friend[0] is the first name, friend[1] is the last name and so on. It could get quite complicated.
This is exactly the kind of problem that a hash can help you with. This is how you define a hash:
friend = {
|
Just like arrays, you can later add another field like this:
friend["country"] = "Canada" |
| Warning: Notice that you create hashes using {squigly brackets} instead of the [square brackets] used for arrays. |
Some terminology: keys and values
Since hashes don't use numeric indices, we use the word keys instead. The objects that the keys refer to are called values.
So, in the above example, the keys are "first name", "last name" and "city" are some of the keys. Their respective values are "Jeffrey", "Biggs" and "Toronto".
Iterating through hashes
Hashes also have iterators. They actually have a few.
Hash#each
Hashes have a Hash#each method, similar to Array#each. This method, however, supplies both they key and the value.
friend.each do |key, value| |
This produces:
city => Toronto |
This points to one drawback of hashes. The data in a hash is not organized in any particular order. Is there any reason why Ruby should know that "first name" comes before "city"? Not at all.
The best way to deal with this sort of problem is by creating your own class which takes care of the problem. Ruby is an excellent language for creating your own classes. But before we do that, there is some material that we should cover. Creating classes and methods is the topic of the next chapter.
Hash#each_pair is a synonym for Hash#each.
Hash#each_key
The name says it all. This method iterates through all the keys just like Arrah#each.
>> friend.each_key do |key| |
Hash#each_value
Iterates through all the values.
>> friend.each_value do |val|
?> puts val
>> end
Toronto
Biggs
Canada
Ontario
34 Airport Rd
Jeffrey
20 Random Tutorials from the same category :
Writing iterators In RUBY
The Scalability of Ruby
Arrays In RUBY
Implementing AddressBook In RUBY
Example: Addressbook RUBY
Iterators In RUBY
Difference of Gtk+ and Ruby/Gtk
Printing the addressbook RUBY
Classes and methods in RUBY
More features IN RUBY
Hashes In RUBY
Progress Bars with GD2 and Ruby
Sorting the addressbook RUBY
What can ruby arrays do?













