What are the most common Ruby array methods?

What are the most common Ruby array methods?

6 mins read
Oct 31, 2025
Share
editor-page-cover

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.


Learn Ruby

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

Learn Ruby

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:

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

2. The new keyword

You can also create a new array using array.new. 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:

Ruby
array = Array.new
#=> []
Array.new(4)
#=> [nil, nil, nil, nil]
Array.new[4, false]
#=> [false, false, false, false]

Bang methods and destructive vs non-destructive behavior#

Many Ruby methods have destructive versions ending with ! that modify the original array, while the non-bang version returns a new array. For example:

arr = [1, 2, 2, 3]
arr.uniq       # => [1, 2, 3]
arr            # => [1, 2, 2, 3]

arr.uniq!      # modifies arr in place
arr            # => [1, 2, 3]

Destructive methods are useful when you want to avoid creating extra copies for performance, but be cautious—they can lead to side effects if you hold references to the original.
Always know whether a method modifies the array in place before using it.


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.

.concat#

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

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

Ruby
languages = ["Ruby", "Python", "Java"]
languages.delete("Python")
#=> ["Ruby", "Java"]

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

Ruby
languages.delete_at(1)
#=> ["Ruby", "Java"]

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

.drop#

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

Ruby
array = [1, 2, 3, 4, 5, 6, 7, 8]
array.drop(3)
#=> [4, 5, 6, 7, 8]

.each#

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

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

.empty?#

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

Ruby
colors = ["blue", "green", "red"]
colors.empty?
#=> false

.first and .last#

The first method returns the first element of the array.

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

The last method returns the last element of the array.

Ruby
dogs = ["Daschund", "Poodle", "Pug"]
dogs.last
#=> Pug

.join#

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

Ruby
array.join
#=> "135"
array.join(“*”)
#=> "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


.length#

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

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

.map#

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

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

.push and .pop#

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

Ruby
array = ["seconds", "minutes"]
array.push("hours")
#=> ["seconds", "minutes", "hours"]

The pop method removes the last element of the array.

Ruby
array = ["seconds", "minutes", "hours"]
array.pop
#=> ["seconds", "minutes"]

.reverse#

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

Let’s say your original array looks like this:

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

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

Ruby
arr.reverse
#=> [9, 7, 5, 3, 1]

.rindex#

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

Ruby
array = [1, 2, 2, 3, 4, 5, 5]
array.rindex(5)
#=> 6
array.rindex(7)
#=> nil

.shift and .unshift#

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

Ruby
jewelry = ["necklace", "ring", "earring"]
jewelry.shift
#=> ["ring", "earring"]

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

Ruby
jewelry = ["necklace", "ring", "earring"]
jewelry.unshift("bracelet")
#=> ["bracelet", "necklace", "ring", "earring"]

.sort#

The sort method sorts the elements from least to greatest.

Ruby
numbers = [10, 5, 25, 15]
numbers.sort
#=> [5, 10, 15, 25]

.take#

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

Ruby
array = [1, 2, 3, 4, 5, 6]
array.take(4)
#=> [1, 2, 3, 4]

.uniq#

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

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

Enumerable methods and array processing helpers#

Because arrays include the Enumerable mixin, you can use higher-level methods for querying and aggregating:

  • select / filter: picks elements matching a predicate
  • reject: opposite of select
  • find / detect: returns first matching element
  • all?, any?, none?, one?: boolean queries
  • inject / reduce: fold / accumulate values
  • sum, max, min, count: statistical helpers

Example usage:

scores = [82, 95, 47, 70, 88]
passing = scores.select { _1 >= 60 }
total = scores.reduce(0, :+)    # sum of all
any_perfect = scores.any? { _1 == 100 }

Also, each_with_index, with_index give index alongside values.
Combine them:

array.each_with_index.map { |v, i| [i, v] }

to produce index-value pairs.


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

Flattening, combining, slicing and advanced patterns#

Let’s push the envelope with some advanced array transformations:

  • flatten: collapse nested arrays into one dimension
  • transpose: flip rows and columns for arrays of arrays
  • zip: combine multiple arrays element-wise
  • product: form Cartesian product across arrays
  • combination / permutation: enumerate subsets or orderings
  • rotate: shift array elements circularly
  • partition: split into two arrays by predicate
  • slice / range indexing: arr[2,3], arr[-3..-1]

Example:

matrix = [[1,2], [3,4]]
matrix.transpose            # => [[1,3], [2,4]]

[1,2,3].zip(['a','b','c'])   # => [[1, 'a'], [2, 'b'], [3, 'c']]

[1,2,3].combination(2).to_a  # => [[1,2], [1,3], [2,3]]

nested = [1, [2, [3,4]], 5]
nested.flatten               # => [1,2,3,4,5]

These methods let you express complex transformations in a small amount of declarative code.

Performance tips and avoiding allocations#

When chaining many array operations, each step may produce a new intermediate array, which costs memory and CPU. To optimize:

  • Use destructive (bang) methods when safe (map!, select!)
  • Combine operations with Enumerable#lazy for large datasets (array.lazy.map.filter.take)
  • Avoid creating large temporary arrays frequently in loops
  • Use each_slice or each_cons for sliding window or chunk processing without extra arrays
  • Be mindful of flatten depth and avoid unnecessary nesting

These tweaks help keep your Ruby array manipulations efficient at scale.

Happy learning!


Continue reading about Ruby#


Written By:
Erin Schaffer