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:
- Using the [] constructor
- 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.
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.
To create an empty array using the literal constructor, pass an empty block as:
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:
To specify the size of the array during creation, pass the size as an integer argument as shown:
The above syntax initializes an array of size 10. By default, the method will populate the array with nil values.
=> [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:
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:
- Push
- Unshift
- Prepend
- Insert
Push method
The push method takes the item you provide and appends it to the end of the array. Consider the fruits array:
To add a new fruit to the array, we can do:
=> ["apple", "oranges", "mangoes", "tomatoes", "melon", "grapes"]
Unshift/prepend
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.
=> ["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[0] = "apples"
Closing
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.