Chapter 9: Arrays

Arrays are ordered lists of values grouped under a common name.

Arrays represent a list of elements. They are very common in programming and are one of the fundamental building blocks of programs. In this article, we’ll learn how they work in Ruby.

So far we have seen variables which store a single entity, e.g. name = 'Akshay' and such. What if you want to store multiple names?

Imagine you want to save the names of 10 students in a class. One approach is to store each name in a separate variable, like this:

student_one = 'Mark'
student_two = 'Steve'
# ...
student_ten = 'Sean'

A different, and better approach is to use a list of names, also known as an Array. When using arrays, you create a single variable that stores a list of data you want to store.

names = [ 'Mark', 'Steve', ..., 'Sean']

Now you might be wondering how you can access the name of the third student. For this, we use an array index, which is just a number to refer to the position of that item. In most programming languages, array indices start from 0. So the first element has an index of 0, second has 1, and so on.

In our case, the third student has an index of 2. Once we have the index (i) and the array name, we’ll use the array[i] notation to refer to the item at that index. To access the third name in the array names, we’ll use:

print names[2]

There are multiple ways to create arrays in Ruby.

numbers = [1, 2, 3, 4]

grades = ['a', 'b', 'c']

# You can create a new empty array as follows:
list = Array.new()  # []
names = []

If you’re coming from a language like C# or Java, it might surprise you that a Ruby array can hold values of any type, since Ruby is a dynamic language.

# an array containing a character, a number, and a symbol
values = [ 'a', 10, :rails ]
list = ['a', 3, 'word', 7]

However, it’s not very common practice. In practice, arrays will hold values of the same type.

An array can even hold other arrays.

coordinates = [4, 7, [3, 5]]

To access the array elements, you have to provide the position of that element, called index. In programming, array positions start with 0, not 1.

numbers = [53, 75, 67, 95]

print numbers[0]  # 53
print numbers[1]  # 75
print numbers[2]  # 67
print numbers[3]  # 95

Ruby provides conveniently named first and last methods to access the first and last elements of the array.

print numbers.first  # 53
print numbers.last  # 95

To add new elements at the end of an existing array, use the << notation. To add it at the front, use the unshift method.

numbers = [53, 75, 67, 95]
numbers << 83
print numbers  # [53, 75, 67, 95, 83]
numbers.unshift(10)
print numbers  # [10, 53, 75, 67, 95, 83]

To count how many items there are in an array, use the length, count, or size method.

languages = ['Ruby', 'PHP', 'Java', 'CSharp', 'Go']

print languages.length  # 5
print languages.count  # 5
print languages.size  # 5

To check if an array contains an item or not, use the include? method.

languages = ['Ruby', 'PHP', 'Java', 'CSharp', 'Go']

languages.include? 'Python'  # false
languages.include? 'Go'  # true

To find the position of an element in an array, use the index method. If that element does not exist in the array, Ruby returns nil value.

languages = ['Ruby', 'PHP', 'Java', 'CSharp', 'Go']

languages.index 'PHP'  # 1
languages.index 'Python'  # nil

In addition to these methods, there are many other useful methods you can use on the Array. For a complete reference, check out the Array documentation.

Next: Hash