Most common Ruby array methods every dev should know

May 21, 2021 - 4 min read
Erin Schaffer

Arrays are objects that store other objects. You can think of an array as a bag that you can throw items in. The bag itself is an object, along with the other items you store inside the bag. Ruby arrays are a very popular data structure, and they can be used to store many different data types. Today, we’re going to take a look at some of the common Ruby array methods every dev should know.

We’ll cover:

Learn Ruby from scratch

This course covers fundamental Ruby topics such as variables, conditionals, built-in classes, objects, and much more.

Learn Ruby from Scratch

What are array methods in Ruby?

Array methods in Ruby are essentially objects that can store other objects. You can store any kind of object in an array. You can create an array by separating values by commas and enclosing your list with square brackets.

In Ruby, arrays always keep their order unless you do something to change the order. They are zero-based, so the array index starts at 0.

There are many different things you can do with arrays such as:

  • Addition
  • Subtraction
  • Intersection
  • And much more

Before we dive into some fundamental Ruby array methods, let’s take a look at two ways you can create a Ruby array.

1. The literal constructor

You can create a new array using the literal constructor:

array = [1, 2, 3, 4, 5]

2. The new keyword

You can also create a new array using You can either create it as an empty array, create it with an initial size, or create it with two arguments (the initial size and the default object). The syntax is shown below:

array = 
#=> [] 
#=> [nil, nil, nil, nil][4, false] 
#=> [false, false, false, false]

Common array methods

There are a lot of array methods built into Ruby’s array class. Let’s take a look at some of the most common methods.


The concat method appends elements from an array to the original array.

array = [1, 3, 5, 7]
array.concat([9, 11, 13])
#=> [1, 3, 5, 7, 9, 11, 13]

.delete and .delete_at

The delete method deletes an element by name.

languages = ["Ruby", "Python", "Java"]
#=> ["Ruby", "Java"]

The delete_at method permanently deletes an element at a specified index.

#=> ["Ruby", "Java"]

Note: There is also .delete_if method, which conditionally deletes elements of an array.


The drop method returns the elements after n elements have been dropped.

array = [1, 2, 3, 4, 5, 6, 7, 8]
#=> [4, 5, 6, 7, 8]


The each method works on objects that allow for iteration. This method iterates through each element in the array.

cats = ["Waffle", "Coconut"]
cats.each {|cat| puts "Hi, #{cat}""}
#=> Hi, Waffle
#=> Hi, Coconut


The empty? method checks whether the array contains any elements.

colors = ["blue", "green", "red"]
#=> false

.first and .last

The first method returns the first element of the array.

dogs = ["Daschund", "Poodle", "Pug"]
#=> Dachshund

The last method returns the last element of the array.

dogs = ["Daschund", "Poodle", "Pug"]
#=> Pug


The join method returns a string of the elements of the array separated by a separator parameter.

#=> "135"

#=> "1*3*5"

Get hands-on with Ruby

Sharpen your Ruby skills without scrubbing through videos or documentation. Educative’s interactive text-based courses are easy to skim and feature live coding environments - making learning fun and efficient.

Learn Ruby from Scratch


To find your array length, you can use the length method. It will return the number of elements in the array.

friends = ["Foo", "Kathy", "Blake"]
#=> 3


The map method takes an operation and applies it to every element of an array, then returns the modified array.

array = [4, 6, 7]{ | number | number + 5 }
#=> [9, 11, 12]

.push and .pop

The push method allows you to add an element to the end of the array.

array = ["seconds", "minutes"]
#=> ["seconds", "minutes", "hours"]

The pop method removes the last element of the array.

array = ["seconds", "minutes", "hours"]
#=> ["seconds", "minutes"]


The reverse method puts the array into reverse order. The original array remains the same.

Let’s say your original array looks like this:

arr = [1, 3, 5, 7, 9]

When you implement the reverse method, it looks like this:

#=> [9, 7, 5, 3, 1]


The rindex method returns the index of the last element which matches the parameter of rindex. It returns nil if no match is found.

array = [1, 2, 2, 3, 4, 5, 5]
#=> 6

#=> nil

.shift and .unshift

The shift method removes and returns the first element of the array.

jewelry = ["necklace", "ring", "earring"]
#=> ["ring", "earring"]

The unshift method allows you to add an element to the beginning of an array.

jewelry = ["necklace", "ring", "earring"]
#=> ["bracelet", "necklace", "ring", "earring"]


The sort method sorts the elements from least to greatest.

numbers = [10, 5, 25, 15]
#=> [5, 10, 15, 25]


The take method returns the first n elements of the array.

array = [1, 2, 3, 4, 5, 6]
#=> [1, 2, 3, 4]


The uniq method takes an array with duplicate elements and returns a copy of the original array but with the duplicate values removed.

chocolates = ["Snickers", "Twix", "Twix", "Hershey", "KitKat"]
#=> ["Snickers", "Twix", "Hershey", "KitKat"]

Advanced array concepts and next steps

Ruby arrays are an important data structure, and you can use them to store many different data types. Now that you’ve covered some common Ruby array methods, it’s time to learn more about advanced array methods and concepts such as:

  • .with_index and .each_index

  • .flatten

  • Nested arrays

  • Multi-dimensional arrays

  • And much more

To continue learning more about Ruby, check out Educative’s course Learn Ruby from Scratch. This interactive course covers the fundamentals of the Ruby programming language. By the end of the course, you’ll be able to use Ruby with confidence and will have your own Ruby certificate to add to your resume.

Happy learning!

Continue reading about Ruby

WRITTEN BYErin Schaffer

Join a community of 270,000 monthly readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.