Top hash methods in Ruby
Since hashes are an integral part of Ruby, I want to go through some helpful methods that can be quite handy for development projects.
We are going to use the same people
hash that we have been working over the last few sections:
people = {jordan: 32, tiffany: 27, kristine: 10, heather: 29}
Adding elements to a hash
First, we are going to see how to add items to a hash. To do that, add in the following code:
people[:leann] = 42
Now, if you type people
, you can see this new element has been successfully added:

So adding to a hash is similar to adding to arrays. But instead of passing in an index and value, you supply a key and then a value.
Swapping keys and values
Next, we are going to see how to reverse the key/value pair values. For our example, let's imagine that we want to swap out the hash so that the age is the key and the name is the value.
One way to do is to iterate through each pair, store it in a variable, swap them, and update. But, that's slow and more tedious...