Ruby Array Basics

Adding and Removing Elements to/from Ruby Arrays

Arrays are Ruby’s versatile collection objects. Essentially, they store lists of other objects and can be thought of as ordered lists. In this article, we’ll consider adding and removing objects from an array.

An Ordered List

In Ruby, an array stores it’s objects in sequence starting at index (position) zero:

 0  1  2  3  4
[1, 2, 3, 4, 5]

In the above example we have an array of integers, starting at 1 and ending at 5. Each integer’s position in the array is identified by it’s index. Thus, the number 4 is at index 3. We can ask for the value stored at position 3 like so:

arr = [1, 2, 3, 4, 5]
arr[3]
=> 4

It’s important to know that Ruby allows you to collect objects of differing types within an array. Let’s add a hash and nest a new array in our existing array of 5 integers:

arr = [1, 2, 3, 4, 5, {animal: :cat}, ['a', 'b', 'c']]
arr[5]
=> {animal:=>:cat}
arr[6]
=> ["a", "b", "c"]

Adding and Removing Objects

Adding objects to an array can be done a number of ways. To add to the front of an array we can use the #unshift method.

[unshift] Prepends objects to the front of self, moving other elements upwards. [It returns the array.]
arr = [1, 2, 3, 4, 5]
arr.unshift(6)
=> [6, 1, 2, 3, 4, 5]

We can also prepend multiple objects to the front of the array:

arr = [1, 2, 3, 4, 5]
arr.unshift(6, 7)
=> [6, 7, 1, 2, 3, 4, 5]

Having just ruined our nice sequence of numbers, we can get rid of the offending items by using the #shift method.

[shift] Removes the first element of self and returns it (shifting all other elements down by one). Returns nil if the array is empty.
arr = [6, 7, 1, 2, 3, 4, 5]
>> arr.shift
=> [6]
>> arr
=> [7, 1, 2, 3, 4, 5]

To remove multiple objects from the front of an array:

arr = [6, 7, 1, 2, 3, 4, 5]
>> arr.shift(2)
=> [6, 7]
>> arr
=> [1, 2, 3, 4, 5]

Adding objects to the end of an array can be done using the #push method or the append method, which is invoked by the double chevron, <<

Append — Pushes the given object(s) on to the end of this array. This expression returns the array itself, so several appends may be chained together.
arr = [1, 2, 3, 4, 5]
>> arr.push(6)
=> [1, 2, 3, 4, 5, 6]
>> arr << 7
=> [1, 2, 3, 4, 5, 6, 7]

To achieve the opposite effect — removing an object from the end of an array we can call #pop on it.

Pop “removes the last element from self and returns it, or nil if the array is empty”
arr = [1, 2, 3, 4, 5, 6, 7]
arr.pop
=> 7
arr
=> [1, 2, 3, 4, 5, 6]

#pop can be chained and so remove multiple objects, but a simpler way is to pass a parameter to the method:

arr = [1, 2, 3, 4, 5, 6, 7]
arr.pop(2)
=> 6, 7
arr
=> [1, 2, 3, 4, 5]

Naturally, we will also want the ability to add objects to arrays, other than at the front or end. For this task we use #insert. As it’s name implies, insert takes an object and the index in an array in which we want to place it.

Inserts the given values before the element with the given index.
Negative indices count backwards from the end of the array, where -1 is the last element. If a negative index is used, the given values will be inserted after that element, so using an index of -1 will insert the values at the end of the array.
arr = [1, 2, 3, 4, 5, 6, 7]
arr.insert(3, 99)
=> [1, 2, 3, 99, 4, 5, 6, 7]
arr.insert(-2, 'a','b','c')
=> [1, 2, 3, 99, 4, 5, 6, "a", "b", "c", 7]

Note that whatever follows the first comma is considered the parameter, no matter how many elements there are separated by commas.

To remove an element at a specific location, we can call #delete_at on the array with the index of the object we wish to remove passed as a parameter.

[delete_at] Deletes the element at the specified index, returning that element, or nil if the index is out of range.
arr
=> [1, 2, 3, 99, 4, 5, 6, "a", "b", "c", 7]
arr.delete_at(3)
=> 99
arr
=> [1, 2, 3, 4, 5, 6, "a", "b", "c", 7]

Similar can be achieved using #slice!

Deletes the element(s) given by an index (optionally up to length elements) or by a range.
Returns the deleted object (or objects), or nil if the index is out of range.
arr
=> [1, 2, 3, 99, 4, 5, 6, "a", "b", "c", 7]
arr.slice!(3)
=> 99
arr
=> [1, 2, 3, 4, 5, 6, "a", "b", "c", 7]
arr.slice!(6..8)
=> ["a", "b", "c"]
>> arr
=> [1, 2, 3, 4, 5, 6, 7]

I hope you found this useful. Next time we’ll take a look at some more common array methods.

Like what you read? Give Jamie Finlay a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.