Arrays And Hashes In Ruby
posted on October 26, 2015
What are arrays and hashes?
In programming, Arrays and Hashes are kinds of objects called collections. Collections are a kind of data structure. What gets collected in these collections? All kinds of objects such as but not limited to numbers (integers and floats), Strings, variables, booleans, classes, other Arrays, and other Hashes may get stored in these collection.
What are the main differences between arrays and hashes?
The main difference between arrays and hashes is that the elements of arrays are stored, identified, and accessed by a consistent sequential numerical index. The contents of an array always remain in the same order, unless you change it. Meanwhile, Hashes are associative arrays, meaning that the elements of hashes are keys associated with values. In key-value pairs, the elements are stored, identified, and accessed by their keys. The keys of a Hash in Ruby can be any object type, including Strings and symbols.
Arrays are ordered collections of objects. Hashes are dictionaries where a key points to a value (just like a word points to a definition in a dictionary) and are ordered in more recent versions of Ruby (as of Ruby version 1.9). Here "ordered" just means that Hashes "know what their first, last, or nth element is". The order in which keys were inserted into a Hash is the order in which the Hash replays itself for you if you iterate through the pairs in it or print a string representation of it to the screen.
JUST TO BE CLEAR: the first key of a Hash can be anything, but the first index of an Array must ALWAYS be ZERO!!!
How are Arrays structured?
Arrays are structured like this: (animals
is an array of Strings
(names of animals).) animals = ["Maru", "Hana", "Boo", "Ohagi", "Goro", "Tama", "Nitama", "Grumpycat", "Li'l Bub"]
. Notice that each element is separated by a comma. To create an empty array, the syntax is like this: my_array = []
. You can also create an Array with constructors: constructed_array = Array.new
creates an empty array, and to make a non-empty array, the syntax is like this: greetings = Array.new("Hello", "Good day")
.
What does it mean to iterate over an array?
To iterate over an array means to go through each element in the order that it is positioned in the array. Because Arrays are indexed numerically and sequentially, the contents of an array always remain in the same order, unless you change it. This means that the position of each element can be computed from its index by a mathematical formula, in order to iterate through and/or access the elements of an array.
Based on what you've learned, what is the best way to access values in an Array?
To access an element of an array, you would need the index (number) of the position where the element is stored. From the above example: if I type animals[0]
, I would get Maru
which is the FIRST element of the Array animals
. If I typed animals[4]
, I would get Goro
, which is the fifth element of animals
. Notice that the FIRST element is element zero, NOT element 1. (FYI, I'm not trying to reference that movie or that game. It just came out that way lol!)
This is because the index of arrays start at 0 and end at the length of the array minus one. For example, in this array ["A", "B", "C", "D"]
, the length of the array is 4 because there are 4 elements inside it. But the last index which "D" occupies is actually the 3rd index, since "A" occupies element 0 (Zero).
Trying to access an element that is not there will usually give an error message in other programming languages like C++. Fortunately, Ruby is more forgiving and this will just output a blank. (Still, not what you'd expect if you want to access "D" from that array.
How are Hashes structured?
To create a Hash in Ruby, there are several ways to do these:
Constructor Method:
empty_hash = Hash.new
my_hash = Hash.new("The Default")
Hash Literal Notation:
empty_hash = {}
my_hash = { # each element is separated by a comma "key1" => value1, # using a String as a key :key2 => value2, # using a symbol as a key 3 => value 3, # using an integer as a key key4: value4 # as of Ruby 1.9, this is shorthand # The key is automatically generated into a symbol. }
Based on what you've learned, what is the best way to access values in a hash?
To access the values inside a hash, one would need to get the key that identifies that value. For example, I have:
grades = { :Bernice => 90, :Madelein => 99, :Audris => 98 }
To get Madelein's grade, I'll type grades[:Madelein]
. This will get me 99.
What are some important things to remember about hash keys and uniqueness?
The keys of a Hash must be unique for the hash they belong. If a hash key already exists inside the given hash, and you could overwrite the answer by accident. For example with our hash grades[:Bernice] already exists, so if I do grades[:Bernice] = 79, I replaced 90 with 79, so now the hash will say:
grades = { :Bernice => 79, :Madelein => 99, :Audris => 98 }
Notice that the value of grades[Bernice]
has changed, instead of creating a new element. But if the key does not exist, then a new key-value pair will be created: grades[:Diane] = 88
will make the hash:
grades = { :Bernice => 79, :Madelein => 99, :Audris => 98, :Diane => 88 }
Arrays and Hashes seem so complicated! Why would I want to use arrays and hashes if I already have good ol' fashioned variables?
For the same reason that you don't get a new bookshelf for each book that you have. Let's go back to animals
. Imagine if we created separate variables for each of those animals' names. There are 9 animal's names in that Array. To make individual variables for them, you would make animal_1 = "Maru"
, animal_2 = "Hana"
, animal_3 = "Boo"
, animal_4 = "Ohagi"
, etc. That's really tedious and tiresome!!! Do you really want to do that? DO YOU!?!?!???!! o_o
Aside from that, with Arrays and Hashes, you can do all sorts of neat things to your data, like sorting, transforming, etc., which will be much more complicated to do with individual variables.
When do you think one will be better over the other?
Neither one is better than the other, because it depends on what they are going to be used for. If the order matters, then it's time to use an array, since the elements are stored linearly/sequentially. An array allows its elements to be randomly accessed, as long as the index of that element is known beforehand. If you'd prefer efficiency/speed of getting the data because the order does not matter, and you'd rather access the data by some keyword, then a hash would be more appropriate.
Here are additional helpful links:
- Ruby-Doc: Array
- Ruby-Doc: Hash
- Codecademy Ruby Glossary
- Wikipedia: Array Data Sructure
- Wikipedia: Associative Array
- Wikipedia: Associative Array
- Rubymonk.com ~ Arrays
- Rubymonk.com ~ Hashes in Ruby
- Rubymonk.com ~ Hashes
- Rubymonk.com ~ Hashes, in and out.
- Wikipedia: Hash Function
- Wikipedia: Hash Table
- Wikipedia: Hash List
- Wikipedia: Hash Tree