THE BLOG

Ruby Data Structures 101: Arrays and Hashes

June 28, 2015

Handling collections of data efficiently is one of the most important things any general-purpose programming language should be able to do. Whether the collection of data is small or large, leveraging the power of a computer to process it sure beats doing everything by hand. Different programming languages provide different (though generally similar) tools for structuring data. Ruby gives its users two powerful built-in data structures for handling collections of data: arrays and hashes. This post presents a basic overview of both structures and examples of their proper use. Also, there will be two atrocious, cringe-worthy puns. You’ve been warned.

Array of Hope

Arrays in Ruby are used to represent any collection of objects, and in particular, any collection whose order is important. Let’s say Bobby’s pre-algebra teacher wants to store his five test grades for storage and computational purposes. Hopefully for Bobby’s sake, his grades will look something like this:

bobby_grades = [99, 94, 100, 97, 98]

Now if Bobby’s teacher wants to quickly access his star pupil’s grade on the first test, it’s as simple as typing:

bobby_grades[0]
# Result: 99
# (As in many programming languages, counting in Ruby starts from 0.)

And if the teacher wants to find Bobby’s average grade, something like this would accomplish it:

bobby_grades.inject(0) { |sum, grade| sum + grade}.fdiv(bobby_grades.length)
# Result: 97.6

While this particular example of an array used a collection of integers, arrays in Ruby can take a collection of any type of object (strings, booleans, classes, other arrays) and even mix object type (though this is not usually recommended).

Making a Hash of It

In contrast to arrays, hashes in Ruby store pairs of data, and each pair consists of a key and value. A dictionary is a classic example of a situation where a hash should be utilized:

devils_dictionary = {
  "learning" => "The kind of ignorance distinguishing the studious",
  "Mammon" => "The god of the world's leading religion"}

Or say you’re designing a video game and you want to assign attributes to your childhood hockey hero:

joe_sakic = {"skating" => 95, "passing" => 97, "wrist_shot" => 99, "hair" => 72}

With hashes, retrieving a stored value is done by referencing its key:

joe_sakic["hair"]
# Result: 72

In versions of Ruby up to and including 1.8.x, hashes were unordered collections, which represented another crucial distinction from arrays. In practice though, since values in hashes are generally accessed by their keys and not by a numerical index (as elements in arrays are), it’s still feasible to think of arrays as more explicitly and obviously ordered than hashes.

Summary

Use arrays for a collection of objects; use hashes for a collection of object pairs. Or, to give one final example:

Array

bad_puns = ["Array of hope", "Make a hash of it"]

Hash

lameness_level = {"Array of hope" => 99, "Make a hash of it" => 92}

Onwards and upwards, fellow Rubyists!