A Hash is a Collection that can also hold multiple values in Key and Value pairs. In the Hash, the elements are unordered, unindexed, changeable and doesn't allow duplicate values.
You can think of a Hash like an actual English Dictionaries. Where you search for a word and you get its explanation.
The declaration of a Hash is also quite simple. You can place the multiple values inside braces {} in the form of Key and Value pairs. And the Key and Value pairs should be separated by =>.
Just imagine the Key to be the word you are going to search in the English Hash. And the Value is the explanation you find in it.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" } puts x
So, in the above code we have created a Hash using square braces {}. And the values are represented in Key and Value pairs.
The Key is a number or a String and in the value we have provided its explanation(Somewhat like the English Dictionary).
i.e. We know that the first element 5 is a number. So, in the Key, we have put the number 5 and in its value, we have put the explanation, i.e. Is a Number. Same logic is applicable for the other two Keys, John and Ruby.
A Key and Value is separated by =>.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" }
And initialised to the variable x.
And in the next line we have printed the Hash using the print statement.
puts x
Now, if we see the output,
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" } for i in x puts i end
Similarly, in the above code we have created a Hash using braces {} and Key => Value pairs.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" }
And initialised to the variable x.
In the next line we have used the for loop to Iterate through the Hash.
for i in x puts i end
As we know, there is a Key and Value pair in a Hash. And naturally, we are just getting Key and Value pair as output.
Let us understand it in the iterations of for loop,
for i in x
In the first Iteration the first Key of the Hash x (i.e. The key 5) and its corresponding value(i.e. Is a Number) is taken and put into the variables i.
And the print statement, prints the value of i(i.e. The key and the value).
5 Is a Number
Similarly, in the second Iteration the second Key of the Hash x (i.e. John) and its corresponding value(i.e. Is a Name) is taken and put into the variables i.
And the print statement, prints the value of i(i.e. The key and the value).
John Is a Name
Similarly, in the third Iteration the third Key of the Hash x (i.e. The key Ruby) and its corresponding value(i.e. Is a Language) is taken and put into the variables i.
And the print statement, prints the value of i(i.e. The key).
Ruby Is a Language
So, we have seen how to get the Key and Value. Now, let us see how can we get only the Value associated with that Key.
It is just like searching a word in an English Hash and get its corresponding value.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" } for i in x.keys puts "The value for the key #{i} is : #{x[i]}" end
Similarly, in the above code we have created a Hash using braces {} and Key => Value pairs.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" }
And initialised to the variable x.
In the next line we have used the for loop to Iterate through the Hash. And used x.keys to get all the keys from the Hash x.
for i in x.keys puts "The value for the key #{i} is : #{x[i]}" end
As we know, there is a Key and Value pair in a Hash. And in each Iteration, we are fetching the Value for a particular Key.
Let us understand it in the iterations of for loop,
for i in x.keys
In the first Iteration the first Key of the Hash x (i.e. The key 5) is taken and put into the variables i.
Then in the print statement, we have used the Key (i.e. 5) to get the value associated to it x[i])
Where,
And the print statement,
puts "The value for the key ",i," is ",x[i])
Prints the Key and Value.
The value for the key 5 is : Is a Number
Similarly, in the second Iteration the second Key of the Hash x (i.e. The key John) is taken and put into the variables i.
Then in the print statement, we have used the Key (i.e. John) to get the value associated to it x[i])
And the print statement,
puts "The value for the key ",i," is ",x[i])
Prints the Key and Value.
The value for the key John is : Is a Name
Similarly, in the third Iteration the third Key of the Hash x (i.e. The key John) is taken and put into the variables i.
Then in the print statement, we have used the Key (i.e. Ruby) to get the value associated to it x[i])
And the print statement,
puts "The value for the key ",i," is ",x[i])
Prints the Key and Value.
The value for the key Ruby is : Is a Language
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" } for i,j in x.each() puts i, j end
Similarly, in the above code we have created a Hash using braces {} and Key => Value pairs.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" }
And initialised to the variable x.
In the next line we have used the for loop to Iterate through the Hash.
for i,j in x.each() puts i, j end
Now, since there is a Key and Value pair in a Hash, two variables i and j are used. i to hold the key and j to hold the value.
And to get all the elements of the Hash, the each() Method is used.
Now, if we see the iterations of for loop,
for i,j in x.each()
In the first Iteration the first value of the Hash x (i.e. The key 5 and the value Is a Number) is taken and put into the variables i and j.
And the print statement, prints the value of i(i.e. The key) and j(i.e. The value).
5 Is a Number
Similarly, in the second Iteration the second value of the Hash x (i.e. The key John and the value Is a Name) is taken and put into the variables i and j.
And the print statement, prints the value of i(i.e. The key) and j(i.e. The value).
John Is a Name
Similarly, in the third Iteration the third value of the Hash x (i.e. The key Ruby and the value Is a Language) is taken and put into the variables i and j.
And the print statement, prints the value of i(i.e. The key) and j(i.e. The value).
Ruby Is a Language
So far, we have seen, how to access all the elements of a Hash. i.e. Both the Key and Value.
Now, what if we want to access just the Key or the Value. Let us see in the next example.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" } for i in x.keys() puts i end
So, in the above code we have created a Hash using braces {} and Key and Value pairs.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" }
And initialised to the variable x.
In the next line we have used the for loop to Iterate through the Hash just to get the Keys.
for i in x.keys() puts i end
Now, since there is a Key and Value pair in a Hash, we have used the keys() Method, that will only print the Keys and not the Values.
And if you see the Output, just the Keys are printed.
Next, let us see, how can we access only Values.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" } for i in x.values() puts i end
So, in the above code we have created a Hash using braces {} and Key and Value pairs.
x = { 5 => "Is a Number", "John" => "Is a Name", "Ruby" => "Is a Language" }
And initialised to the variable x.
In the next line we have used the for loop to Iterate through the Hash just to get the Values.
for i in x.values() puts i end
Now, since there is a Key and Value pair in a Hash, we have used the values() Method, that will only print the Values and not the Keys.
And if you see the Output, just the Values are printed.