Ruby Push Array to the Front

Arrays represent a list of ordered items. Arrays are fundamental in Ruby and programming in general, so much so that they can unlock a huge potential if you understand and use them correctly.

This guide will show you how to carry out various array operations and practical array methods in Ruby.

Initializing an array

There are two primary ways to create an array in Ruby:

  1. Using the [] constructor
  2. Using the new class method.

Using the [] constructor

This is the most common way to create an array in Ruby. Use a pair of square brackets and add the array items in order separated by commas.

["a", "b", "c"]

Each item in the array is known as an array’s element and can be of any valid Ruby object.

For example, you can have an array containing: integers, floats, hashes, nested arrays, and more.

You can also assign a variable to an array using the assignment operator.

x = ["a", "b", "c"]

To create an empty array using the literal constructor, pass an empty block as:

empty = []

Using the new class method

In Ruby, you can create an array using the new method.

You can pass arguments to the new method to specify various properties of the array.

The example below creates an empty array:

empty =

To specify the size of the array during creation, pass the size as an integer argument as shown:

myarray =

The above syntax initializes an array of size 10. By default, the method will populate the array with nil values.

myarray =
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

Accessing Array Elements

To access elements in an array, use the index notation. Each element in the array contains what we call an index. An index is a position that describes where the element is (its location) in the collection.

In Ruby, indexing starts at position 0 from left to right.

Consider the array that contains fruits:

fruits = ["apple", "oranges", "mangoes", "tomatoes", "melon"]

The first element in the fruits array is at index 0. Hence, to get its value, we use its index position as:

=> apple

The index format is shown in the diagram below:

To access elements within a specific position range, you can do:

=> ["mangoes", "tomatoes", "melon"]

The format above will retrieve all the items from index 2 to 4 as specified above.

Adding Elements to an Array

There are various methods you can use to add elements to an array. Such methods include:

  1. Push
  2. Unshift
  3. Prepend
  4. Insert

Push method

The push method takes the item you provide and appends it to the end of the array. Consider the fruits array:

fruits = ["apple", "oranges", "mangoes", "tomatoes", "melon"]

To add a new fruit to the array, we can do:

=> ["apple", "oranges", "mangoes", "tomatoes", "melon", "grapes"]


The unshift method is similar to push, except it adds the specified item at the beginning of the array.

=> ["pineapples", "apple", "oranges", "mangoes", "tomatoes", "melon", "grapes"]

You can also use the prepend method, which is an alias of the unshift method.

=> ["pineapples", "apple", "oranges", "mangoes", "tomatoes", "melon", "grapes"]

Insert method

The insert method is more flexible. It allows you to specify to what index to add the item.

For example, to add an element at the beginning of the array, we can specify the index position to be 0.

fruits.insert(0, "bananas")
=> ["bananas", "apple", "oranges", "mangoes", "tomatoes", "melon"]

Updating an array

Suppose you want to update the value stored in an array? You can use the item’s index position followed by its new value.

For example, in the fruits array, we can update the “apple” element to be “apples” by doing:

fruits = ["apple", "oranges", "mangoes", "tomatoes", "melon"]
fruits[0] = "apples"


This guide shows you how to work with arrays in Ruby and perform operations such as creating an array, adding items to an array, and updating items.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list