Skip to main content
  1. Tutorials/

How To Work with Arrays in Ruby

Tutorials Development Ruby

Introduction #

An array is a data structure that represents a list of values, called elements. Arrays let you store multiple values in a single variable. In Ruby, arrays can contain any data type, including numbers, strings, and other Ruby objects. This can condense and organize your code, making it more readable and maintainable. All arrays are objects with their own methods you can call, providing a standardized way to work with datasets.
In this tutorial, you’ll create arrays, access the values they contain, add, modify, and remove elements in an array, and iterate through the elements in an array to solve more complex problems.

Creating an Array>

Creating an Array #

You’ll start by looking at how to create arrays in more detail. As an example, here is a list of various shark species. Without an array, you might store them in individual variables:

shark1 = "Hammerhead"
shark2 = "Great White"
shark3 = "Tiger"

This approach is verbose and can quickly become difficult to maintain, as it’s not very flexible. Adding another shark means you’d have to add, and track, an additional variable.
If you use an array, you can simplify this data. To create an array in a Ruby program, use square brackets: ([]), and separate the values you want to store with commas:

sharks = ["Hammerhead", "Great White", "Tiger"]

Instead of creating three separate variables, you now have one variable that contains all three sharks. In this example, you used square brackets — [] — to create an array, and separated each entry with a comma. If you had to add an additional shark, you would add another shark to the array rather than creating and managing a new variable.
You can print out an entire array with the print statement, which will display the array’s contents:

print sharks
["Hammerhead", "Great White", "Tiger"]

If you want to create an array where each entry is a single word, you can use the %w{} syntax, which creates a word array:

days = %w{Monday Tuesday Wednesday Thursday Friday Saturday Sunday}

This is equivalent to creating the array with square braces:

days =  ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

However, notice that the %w{} method lets you skip the quotes and the commas.
Arrays are often used to group together lists of similar data types, but in Ruby, arrays can contain any value or a mix of values. This includes other arrays. Here’s an example of an array that contains a string, a nil value, an integer, and an array of strings:

record = [

Now you’ll look at how to access data stored in arrays.

Accessing Items in Arrays>

Accessing Items in Arrays #

To access a specific item, or element of an array, you reference its index, or its position in the array. In Ruby, indexes start at zero. In order to retrieve the first element from your sharks array, you append the element’s index to the variable using square brackets:

sharks = ["Hammerhead", "Great White", "Tiger"]

The sharks array has three elements. Here is a breakdown of how each element in the sharks array is indexed.

Great White


The first element in the array is Hammerhead, which is indexed at . The last element is `Tiger`, which is indexed at `2`. Counting starts with in indices, which goes against the natural intuition to start counting at 1, so you’ll want to keep this in mind until it becomes natural.

Note: It might help you to think of the index as an offset, meaning the number of places from the start of the array. The first element is at the beginning, so its offset, or index, is ``. The second element is one spot away from the first entry in the array, so its offset, or index, is 1.

You can find out how many elements are in an array with the length method:


Although the indices of sharks start at `` and go to 2, the length property returns the number of elements in the array, which is 3. It’s not concerned with the indices at all.
If you wanted to find out the index number of a specific element in an array, such as Tiger, use the index() method:

print sharks.index("Tiger")

This returns the index of the first element containing that text. If an index number is not found, such as for a value that does not exist, the console will return nil:

print sharks.index("Whale")

To get the last element of an array in Ruby, use the index -1:

print sharks[-1]

Ruby also provides the first and last methods to get the first and last elements without using indices:

puts sharks.first
puts sharks.last

Attempting to access an index that doesn’t exist will return nil:


Arrays can contain other arrays, which are called nested arrays. This is one way to model two-dimensional datasets in a program. Here’s an example of a nested array:

nested_array = [

In order to access elements in a nested array, you would add another index number to correspond to the inner array. For example, to retrieve the value coral from this nested array, you’d use the following statement:

print nested_array[1][0];

In this example, you accessed the array at position 1 of the nested_array variable, which returned the array ["coral", "reef"]. You then accessed the elements at position `` of that array, which was "coral".
Now you’ll look at how to add elements to an array.

Adding Elements to Arrays>

Adding Elements to Arrays #

You have three elements in your sharks array, which are indexed from `` to 2:

sharks = ["Hammerhead", "Great White", "Tiger"]

There are a few ways to add a new element. You could assign a value to the next index, which in this case would be 3:

sharks[3] = "Whale";

print sharks
["Hammerhead", "Great White", "Tiger", "Whale"]

This method is error-prone though. If you add an element and accidentally skip an index, it will create a nil element in the array.

sharks[5] = "Sand";

print sharks;
["Hammerhead", "Great White", "Tiger", "Whale", nil, "Sand"]

Attempting to access the extra array element will return its value, which will be nil:


Finding the next available index in an array is error-prone and takes extra time. Avoid errors by using the push method, which adds an element to the end of an array:

print sharks
["Hammerhead", "Great White", "Tiger", "Whale", nil, "Whale", "Thresher"]

You can also use the << syntax instead of the push method to add an element to the end of an array:

sharks << "Bullhead"
["Hammerhead", "Great White", "Tiger", "Whale", nil, "Whale", "Thresher", "Bullhead"]

To add an element to the beginning of an array, use the unshift() method:

print sharks
["Angel", "Hammerhead", "Great White", "Tiger", "Whale", nil, "Whale", "Thresher", "Bullhead"]

Now that you know how to add elements, you’ll look at removing them.

Removing Elements from Arrays>

Removing Elements from Arrays #

To remove a specific element from an array, use the delete or delete_at methods. In the sharks array, you created a nil array element earlier. Now, you’ll get rid of it.
First, find its position in the array. You can use the index method to do that:

print sharks.index(nil)

Then use delete_at to remove the element at index 4 and print the array:

print sharks
["Angel", "Hammerhead", "Great White", "Tiger", "Whale", "Thresher", "Bullhead"]

The delete method removes elements from an array that match the value you pass in. Use it to remove Whale from the array:

print sharks;
["Angel", "Hammerhead", "Great White", "Tiger", "Thresher", "Bullhead"]

The delete method will remove all occurrences of the value you pass, so if your array has duplicate elements, they’ll all be removed.
The pop method will remove the last element in an array:

print sharks;
["Angel", "Hammerhead", "Great White", "Tiger", "Thresher"]

Bullhead has been removed as the last element of the array. In order to remove the first element of the array, use the shift method:

print sharks
["Hammerhead", "Great White", "Tiger", "Thresher"]

This time, Angel was removed from the beginning of the array.
By using pop and shift, you can remove elements from the beginning and the end of arrays. Using pop is preferred wherever possible, as the rest of the items in the array retain their original index numbers.
The delete_at, pop, and shift methods all change the original array and return the element you deleted::

sharks = ["Hammerhead", "Great White", "Tiger", "Whale"]
deleted_at_element = sharks.delete_at(1)
popped_element = sharks.pop

puts "Deleted_at element: #{deleted_at_element}"
puts "Popped element: #{popped_element}"

puts "Remaining array: #{sharks}"
Deleted_at element: Great White
Popped element: Whale
Remaining array: ["Hammerhead", "Tiger"]

You now know several ways to remove elements from an array. Now you’ll look at how to modify the element you already have.

Modifying Existing Elements in Arrays>

Modifying Existing Elements in Arrays #

To update an element in the array, assign a new value to the element’s index by using the assignment operator, just like you would with a regular variable.
Given a new array of sharks, with "Hammerhead" at index ``, you’ll replace "Hammerhead" with "Angel":

sharks = ["Hammerhead", "Great White", "Tiger", "Whale"]
sharks[0] = "Angel"
print sharks;
["Angel", "Great White", "Tiger", "Whale"]

To make sure you update the right element, you could use the index method to locate the element first, just like you did to find the element you wanted to delete.
Now you’ll look at how to work with all of the elements in the array.

Iterating Over Arrays>

Iterating Over Arrays #

Ruby provides many ways to iterate over an array, and each method you use depends on the kind of work you want to perform. Next you’ll explore how to iterate over an array and display each of its elements.
Ruby provides the syntax:

sharks = ["Hammerhead", "Great White", "Tiger", "Whale"]
for shark in sharks do
  puts shark

For each element in the sharks array, Ruby assigns that element to the local variable shark. You can then print the element’s value using puts.
You won’t see very often though. Ruby arrays are objects, and they provide the each method for working with elements. The each method works in a similar fashion to, but has a different syntax:

sharks = ["Hammerhead", "Great White", "Tiger", "Whale"]
sharks.each do |shark|
  puts shark

The each method uses a syntax you’ll see often in Ruby programming. It takes a Ruby block as its argument. A block is some code that will be executed later in the context of the method. In this case, the code is puts shark. The shark keyword, enclosed in the pipe characters (|), is the local variable that represents the element in the array that the block will access. Ruby assigns the element to this variable and executes the code in the block. The each method repeats this process for each element in the array:

Great White

When the block is only a single line, you often see Ruby developers replace the do and end keywords with curly braces and condense the whole statement into a single line:

sharks.each {|shark| puts shark }

This produces the same results but uses fewer lines of code.
The each_with_index method works in a similar manner, but it also gives you access to the index of the array element. This program uses each_with_index to print out the index and the value for each element:

sharks = ["Hammerhead", "Great White", "Tiger", "Whale"]
sharks.each_with_index do |shark, index|
  puts "The index is #{index}"
  puts "The value is #{shark}"

For each element in the array, Ruby assigns the element to the variable shark, and assigns the current index to the index variable. You can then reference both of those variables in the block.

The index is 0
The value is Hammerhead
The index is 1
The value is Great White
The index is 2
The value is Tiger
The index is 3
The value is Whale

You’ll iterate over the elements in an array often in your own programs, such as when you need to display the items from a database on a website, or when you’re reading lines from a file and processing their contents.


Conclusion #

Arrays are an extremely versatile and fundamental part of programming in Ruby. In this tutorial, you created arrays and accessed individual elements. You also added, removed, and modified elements in an array. Finally, you explored two ways to iterate over an array and display its contents, which is used as a common method to display data.
Learn about other data types in Ruby by reading the tutorial Understanding Data Types in Ruby.