Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Arrays in Ruby?

In this blog, we are going to learn about arrays in Ruby, a powerful and versatile programming language. If you are new to programming, don't worry! We will try to avoid jargon as much as possible, and if we do use any, we will explain it in a simple and easy-to-understand way. We will provide actual code examples and use everyday analogies to help you understand the concept of arrays.

What are Arrays?

Let's start with an analogy. Imagine you have a row of boxes, and you want to store different items in each box. You would number the boxes to keep track of what's inside them. In programming, we use a similar concept to store data. We call this row of boxes an "array".

An array is a data structure that stores a collection of values or objects. Each value or object in the array is called an "element". Arrays are useful because they allow you to store multiple values in a single, organized data structure.

In Ruby, arrays are ordered, meaning that the elements are stored in a specific order. The order is important because each element in the array is assigned a unique number called an "index". The index is used to access and manipulate the elements in the array.

Creating an Array in Ruby

There are several ways to create an array in Ruby. Let's look at some examples:

Using Square Brackets

One of the simplest ways to create an array is by using square brackets []. You can create an empty array or an array with elements inside it:

# Creating an empty array
numbers = []

# Creating an array with elements
fruits = ["apple", "banana", "cherry"]

In the fruits array example, we have an array with three elements: "apple", "banana", and "cherry".

Using the Array.new Method

Another way to create an array is by using the Array.new method. This method allows you to create an array with a specific size and, optionally, a default value for each element:

# Creating an array with a specific size (3) and a default value (0)
numbers = Array.new(3, 0) # Output: [0, 0, 0]

# Creating an array with a specific size (3) and no default value
empty_array = Array.new(3) # Output: [nil, nil, nil]

In the numbers array example, we created an array with three elements, and each element has a default value of 0.

Accessing Array Elements

As we mentioned earlier, each element in an array has a unique index number. Index numbers start from 0. To access an array element, you simply use the array name followed by the index number in square brackets:

fruits = ["apple", "banana", "cherry"]

puts fruits[0] # Output: apple
puts fruits[1] # Output: banana
puts fruits[2] # Output: cherry

You can also use negative index numbers to access elements from the end of the array:

fruits = ["apple", "banana", "cherry"]

puts fruits[-1] # Output: cherry
puts fruits[-2] # Output: banana
puts fruits[-3] # Output: apple

If you try to access an element with an index that is outside the bounds of the array, Ruby will return nil:

fruits = ["apple", "banana", "cherry"]

puts fruits[3] # Output: nil

Modifying Array Elements

You can modify the elements in an array by assigning a new value to an existing index:

fruits = ["apple", "banana", "cherry"]

fruits[1] = "orange" # Change "banana" to "orange"

puts fruits.inspect # Output: ["apple", "orange", "cherry"]

In this example, we replaced the element with index 1 (which was "banana") with the new value "orange".

Adding and Removing Elements

Adding Elements

There are several ways to add elements to an array. Here are some examples:

  • Using the << operator:
fruits = ["apple", "banana", "cherry"]

fruits << "orange" # Add "orange" to the end of the array

puts fruits.inspect # Output: ["apple", "banana", "cherry", "orange"]
  • Using the push method:
fruits = ["apple", "banana", "cherry"]

fruits.push("orange") # Add "orange" to the end of the array

puts fruits.inspect # Output: ["apple", "banana", "cherry", "orange"]
  • Using the insert method:
fruits = ["apple", "banana", "cherry"]

fruits.insert(1, "orange") # Insert "orange" at index 1

puts fruits.inspect # Output: ["apple", "orange", "banana", "cherry"]

Removing Elements

There are also several ways to remove elements from an array. Here are some examples:

  • Using the pop method:
fruits = ["apple", "banana", "cherry"]

fruits.pop # Remove the last element ("cherry") from the array

puts fruits.inspect # Output: ["apple", "banana"]
  • Using the shift method:
fruits = ["apple", "banana", "cherry"]

fruits.shift # Remove the first element ("apple") from the array

puts fruits.inspect # Output: ["banana", "cherry"]
  • Using the delete_at method:
fruits = ["apple", "banana", "cherry"]

fruits.delete_at(1) # Remove the element at index 1 ("banana")

puts fruits.inspect # Output: ["apple", "cherry"]
  • Using the delete method:
fruits = ["apple", "banana", "cherry"]

fruits.delete("banana") # Remove the element "banana" from the array

puts fruits.inspect # Output: ["apple", "cherry"]

Iterating Over Arrays

You can easily iterate over the elements in an array using the each method. The each method takes a block of code, which is executed once for each element in the array:

fruits = ["apple", "banana", "cherry"]

fruits.each do |fruit|
  puts "I like #{fruit}!"
end

# Output:
# I like apple!
# I like banana!
# I like cherry!

In this example, the block of code is executed three times (once for each element in the fruits array), and the fruit variable takes on the value of the current element.

Conclusion

In this blog, we learned about arrays in Ruby, a powerful and versatile programming language. Arrays are an essential data structure that allows you to store multiple values in an organized and efficient way. We covered how to create, access, modify, add and remove elements from arrays, as well as how to iterate over arrays.

We hope this blog was helpful to you, and we encourage you to practice using arrays in Ruby to become more familiar and comfortable with this powerful data structure. Happy coding!