Arrays are like lists: they appear inside of square brackets and divide elements up by commas. Arrays are ordered lists in that each element has a consecutive index position. Arrays can contain any type of object, but conventionally a single array should hold just one type of item: integers, for example.

By contrast, hashes are lists of key-value pairs. In order to access a particular value in a hash, you must query for it using its key. Hashes are by definition unsorted and index-less, making it important not to have multiple identical keys pointing to variating values.

The main additional difference between arrays and hashes is that hashes cannot be sorted and remain hashes. To sort a hash is to turn it into an array, where each key-value pair becomes a sub-array with a length of 2. Arrays are almost always a better choice when you are dealing with one kind of item, such as strings, that are each unique and ungrouped; hashes are better when you are dealing with bits of information that are linked to other bits of information, like instances of Client classes with name, email, and address attributes.

There are many interesting methods that Ruby comes built-in with for arrays and hashes. Some of the great ones are normal, everyday methods like #each or #map or #sort, but some are less known. Here are some of my favorite lesser-known hash and array methods.


Array#each_index

#each_index is a variation on #each a wonderful iteration method that returns the object itself and is therefore used primarily for its side effects. #each iterates over a series by picking up each object of the series one at a time. By contrast, #each_index picks up each consecutive index. This is super helpful when iterating over an array with multiple similar values. Example:


Array#include?

Here's another super useful method. This #include? method tests to see if a given argument is present in the array. It's a great way to check if an item you're holding is present in an array, and if so, to do something with it or something else entirely. Example:


Hash#keys or Hash#values

#keys and #values methods do similar things: they produce arrays of the hash's keys or values, respectively. This is an incredibly useful set of methods because turning these items into arrays makes it possible to sort them. This example below doesn't make use of this sorting capability, but it does show how it makes it easy to see if a given key is included in a hash using #keys.include?(object), and shows how calling #values.flatten can produce one large array of the hash's values:


Object#itself

This last method is not strictly an Array or Hash method, but its functionality is to produce a hash with array values, so I thought it would be nice to include it here. At first glance, this method seems silly: why would it be useful to have a method that just returns the value of the item itself? Well, it takes a bit of thinking, but at some point you might realize that it can indeed make your life oddly easier at times...


Hopefully this has been interesting and curiosity-inducing, and made you think about trying these methods out for yourself. Happy Ruby-ing!