Tutorial

How To Work with Arrays in Ruby

Updated on January 27, 2023
How To Work with Arrays in 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

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:

sharks.rb
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.rb
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
Output
["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:

mixed_data.rb
record = [
    "Sammy",
    null,
    7,
    [
        "another",
        "array",
    ]
]

Now you’ll look at how to access data stored 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.rb
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.

Hammerhead Great White Tiger
0 1 2

The first element in the array is Hammerhead, which is indexed at 0. The last element is Tiger, which is indexed at 2. Counting starts with 0 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 0. 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:

sharks.length
Output
3

Although the indices of sharks start at 0 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")
Output
2

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")
Output
nil

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

print sharks[-1]
Output
"Tiger"

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

puts sharks.first
puts sharks.last
Output
"Hammerhead" "Tiger"

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

sharks[10]
Output
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 = [
    [
        "salmon",
        "halibut",
    ],
    [
        "coral",
        "reef",
    ]
]

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];
Output
coral

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 0 of that array, which was "coral".

Now you’ll look at how to add elements to an array.

Adding Elements to Arrays

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

sharks.rb
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
Output
["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;
Output
["Hammerhead", "Great White", "Tiger", "Whale", nil, "Sand"]

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

sharks[4]
Output
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:

sharks.push("Thresher")
print sharks
Output
["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"
Output
["Hammerhead", "Great White", "Tiger", "Whale", nil, "Whale", "Thresher", "Bullhead"]

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

sharks.unshift("Angel")
print sharks
Output
["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

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)
Output
4

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

sharks.delete_at(4)
print sharks
Output
["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:

sharks.delete("Whale")
print sharks;
Output
["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:

sharks.pop
print sharks;
Output
["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:

sharks.shift
print sharks
Output
["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.rb
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}"
Output
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

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 0, you’ll replace "Hammerhead" with "Angel":

sharks = ["Hammerhead", "Great White", "Tiger", "Whale"]
sharks[0] = "Angel"
print sharks;
Output
["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

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 for..in syntax:

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

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 for..in 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 for..in, but has a different syntax:

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

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:

Output
Hammerhead Great White Tiger Whale

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:

each.rb
...
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:

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

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.

Output
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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


Tutorial Series: How To Code in Ruby

Ruby is a popular object-oriented programming language. You can use Ruby to write anything from simple scripts to complex web applications. Open your favorite text editor and follow these tutorials to start exploring Ruby.

About the authors

Default avatar
Tony Tran

author


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.