Blog

Storage Wars


Week 3


Ruby Arrays vs. Hashes


October 2, 2014


In Ruby, arrays and hashes represent two different ways of storing lists of data. Both can be easily accessed to call up a specific element and are used to store user information, contact lists, pricing information, and so on. Although both collect and store information, they differ in their syntax and their uses. Arrays are more useful for straightforward lists and data, for example sorting a list of prices. Hashes are more flexible and are used to link information together, like to store a name and an address together.

Arrays

Arrays are the first type of indexed lists. They are defined with a unique array name set "equal to" a list of data.

Here are a two examples of arrays:


Array_1 = [1, 2, 3, 4, 5]


Array_2 = [“Hi”, “Hola”, “Bonjur”, “Konnichiwa”]


As you can see, arrays can store numbers and strings, but they can also store other arrays, hashes, or other elements. The information in arrays can be accessed by calling the position of the element you want. But it’s important to note that the list of index positions starts at 0 rather than 1. So if you want to call up the number “1” in the first array you would write “puts Array_1[0]”, which would print out “1,” since that is in the “zeroth” position.

Arrays also have many special methods that are used to modify the array or access information. For example, to add an element to an array you type Array_1.push(“new element”). To determine the number of elements in an array, you’d write “Array_1.length”, which would give you “5” because there are 5 elements. To read about the many other types of array methods, check out Ruby Docs.

One very common type of method used on arrays is iterators, which go over each array element specified and modify it. Here is an example:


Array_2.each { |x| puts x + “, Philip” }


This would output the following:


“Hi, Philip"
“Hola, Philip”
“Bonjur, Philip”
“Konnichiwa, Philip”


As you can see, the “.each” method ensures the code block that follows is applied to each element in the array. In the code block, each element is defined using “|x|, ” which is a placeholder name for each element in the array. So, for each “|x|” (element), Ruby will write out (puts) the element plus the “, Philip” string. The result is a personalized greeting in a few different languages. There are many more ruby iterators, such as “.times”, “.map”, and others, which are all explained in Ruby Docs.


Hashes


Hashes are also indexed lists, just like arrays, but they differ in one main way. Instead of having a value that corresponds to an element in the list, hashes use a specific “key” to access the data. You can think of hashes as a dictionary that contains a word (the “key”) and the definition attached to it (the “value”). This specificity makes them more flexible in the way they are used.

Here is a sample hash:


Hash_1 = {
:name => “Philip”
:hometown => “Oakland”
:favorite_color => “Green” }


In the first element, the “:name” is the key, while “Philip” is the element that the key refers to. An element can be called by calling on the key, like so:


Hash_1[name]


Which would output “Philip.” You can also call hashes within a string, like in this example:


“My name is #{Hash_1[name]}”


This of course would print out “Hello, my name is Philip”.

Hashes also have many of the same methods as arrays, such as “.length”, “.delete”, and “.select”. Also like arrays, hashes have iterators that pass a block of code to select certain keys, values, or key/value pairs.

For example:


Hash_1.each {|k,v| v.include?(“O”) puts v}


This would return “Oakland”, the only value that includes the letter “O” in it.


To sum up, arrays are more often used to store lists of pure data that can be easily sorted and ordered. Hashes, on the other hand, are more flexible for data linked to other data. In a hash you can look up a word and find the definition. Both arrays and hashes are essential building blocks in Ruby, and understanding how each are used is crucial to becoming a master coder!