Arrays and Hashes

by Jim O'Neal on Friday, Dec 4, 2015

In this blog I will discuss Arrays and Hashes in Ruby, what they are, what are the differences, and will give some examples of both.

Arrays and hashes have important similarities and important differences. Both arrays and hashes are like storage bins that can hold pieces of information (elements) in specific slots. Both arrays and hashes can be assigned a name, so that the array or hash can be utilized in other parts of the program, and the elements can be accessed individually. Both arrays and hashes can contain strings, numbers, or even other arrays or hashes.

The important difference between an array and hash is how the 'slots' are named. In an array, each slot has an index number, starting at 0 and then increasing by 1's for each element. The elements in an array are accessed by refering to this index number. Hashes on the other hand index by using a 'key' which can be assigned a specific name. This allows the program to access the value in the hash by refering to a name rather than just an index number.

Below are some examples showing the format of an array and how to access elements:

sample_array = ["this", "is", "an", "array"]

return sample_array[0] # "this"
return sample_array[1] # "is"
return sample_array[2] # "an"
return sample_array[3] # "array"

Below is an example of a hash and how to access the values in a hash using the keys:

sample_hash = {
  favorite_number: 13,
  favorite_color: "blue",
  favorite_animal: "elephant",
}

return sample_hash[:favorite_number] # 13
return sample_hash[:favorite_color] # "blue"
return sample_hash[:favorite_animal] # "elephant"