Introduction
At the beginning of this section, you learned how to create and manipulate numbers and strings and how to assign them to variables. In real-world development, where you’ll be working with dozens (and even hundreds!) of variables, working with numbers and strings individually is tedious, if not impossible.
One way Ruby allows you to represent a collection of data is with arrays, which you can think of as ordered lists. Rather than working with individual variables, numbers, or strings, an array allows you to create and manipulate an ordered and indexed collection of these data. The individual variables, numbers, or strings within an array are known as elements. An array can contain any combination of variables, numbers, strings, or other Ruby objects (including other arrays), although it is advisable to keep similar data types in any one array.
Lesson overview
This section contains a general overview of topics that you will learn in this lesson.
- Describe what an array is, and explain why it’s useful.
- Describe two different ways of creating a new array.
- Explain how to access array elements using different methods.
- Explain three different ways to add data to an array.
- Explain how to remove elements from an array.
Creating arrays
Here are two basic arrays:
num_array = [1, 2, 3, 4, 5]
str_array = ["This", "is", "a", "small", "array"]
Both arrays have five elements separated by commas. The first array contains integers, while the second array contains strings.
Arrays are commonly created with an array literal, which is a special syntax used to create instances of an array object. To create a new array using an array literal, use square brackets ([]
).
An array can also be created by calling the Array.new
method. When you call this method, you can also include up to 2 optional arguments (initial size and default value):
Array.new #=> []
Array.new(3) #=> [nil, nil, nil]
Array.new(3, 7) #=> [7, 7, 7]
Array.new(3, true) #=> [true, true, true]
Accessing elements
Every element in an array has an index, which is a numerical representation of the element’s position in the array. Like most other programming languages, Ruby arrays use zero-based indexing, which means that the index of the first element is 0, the index of the second element is 1, and so on. Accessing a specific element within an array is done by calling my_array[x]
, where x
is the index of the element you want. Calling an invalid position will result in nil
. Ruby also allows the use of negative indices, which return elements starting from the end of an array, starting at [-1].
str_array = ["This", "is", "a", "small", "array"]
str_array[0] #=> "This"
str_array[1] #=> "is"
str_array[4] #=> "array"
str_array[-1] #=> "array"
str_array[-2] #=> "small"
Finally, Ruby provides the #first
and #last
array methods, which should be self-explanatory. In addition, these methods can take an integer argument, e.g., my_array.first(n)
or my_array.last(n)
, which will return a new array that contains the first or last n
elements of my_array
, respectively.
str_array = ["This", "is", "a", "small", "array"]
str_array.first #=> "This"
str_array.first(2) #=> ["This", "is"]
str_array.last(2) #=> ["small", "array"]
Adding and removing elements
Adding an element to an existing array is done by using the #push
method or the shovel operator <<
. Both methods will add elements to the end of an array and return that array with the new elements. The #pop
method will remove the element at the end of an array and return the element that was removed.
num_array = [1, 2]
num_array.push(3, 4) #=> [1, 2, 3, 4]
num_array << 5 #=> [1, 2, 3, 4, 5]
num_array.pop #=> 5
num_array #=> [1, 2, 3, 4]
The methods #shift
and #unshift
are used to add and remove elements at the beginning of an array. The #unshift
method adds elements to the beginning of an array and returns that array (much like #push
). The #shift
method removes the first element of an array and returns that element (much like #pop
).
num_array = [2, 3, 4]
num_array.unshift(1) #=> [1, 2, 3, 4]
num_array.shift #=> 1
num_array #=> [2, 3, 4]
It’s also useful to know that both #pop
and #shift
can take integer arguments:
num_array = [1, 2, 3, 4, 5, 6]
num_array.pop(3) #=> [4, 5, 6]
num_array.shift(2) #=> [1, 2]
num_array #=> [3]
Adding and subtracting arrays
What do you think will be the outcome of [1, 2, 3] + [3, 4, 5]
?
If you guessed [1, 2, 3, 3, 4, 5]
, congratulations! Adding two arrays will return a new array built by concatenating them, similar to string concatenation. The concat
method works the same way.
a = [1, 2, 3]
b = [3, 4, 5]
a + b #=> [1, 2, 3, 3, 4, 5]
a.concat(b) #=> [1, 2, 3, 3, 4, 5]
To find the difference between two arrays, you can subtract them using -
. This method returns a copy of the first array, removing any elements that appear in the second array.
[1, 1, 1, 2, 2, 3, 4] - [1, 4] #=> [2, 2, 3]
Basic methods
Ruby gives you many methods to manipulate arrays and their contents (over 150!), many of which are beyond the scope of this lesson. To learn about other methods, go to the official documentation and browse the Array class documentation page, where you can find methods listed alphabetically (by scrolling the left sidebar) or summarized and grouped by purpose (by reading under “What’s Here”).
Calling the #methods
method on an array will also yield a long list of the available methods.
num_array.methods #=> A very long list of methods
Here is a brief look at some other common array methods you might run into:
[].empty? #=> true
[[]].empty? #=> false
[1, 2].empty? #=> false
[1, 2, 3].length #=> 3
[1, 2, 3].reverse #=> [3, 2, 1]
[1, 2, 3].include?(3) #=> true
[1, 2, 3].include?("3") #=> false
[1, 2, 3].join #=> "123"
[1, 2, 3].join("-") #=> "1-2-3"
Assignment
- Read Launch School’s chapter on Arrays, following along with the exercises using irb or any other REPL, such as replit.com.
- Read The Definitive Guide to Ruby Arrays by Jesus Castello.
- Complete the array exercises from the ruby-exercises repo that you previously cloned.
- Let’s get some practice reading documentation. You’ll feel just like a real programmer! What do you think the methods
#clear
,#insert
,#sample
,#shuffle
, and#uniq
do? Look these methods up in the Array class documentation. Were you close?
Knowledge check
The following questions are an opportunity to reflect on key topics in this lesson. If you can’t answer a question, click on it to review the material, but keep in mind you are not expected to memorize or master this knowledge.
Additional resources
This section contains helpful links to related content. It isn’t required, so consider it supplemental.
- If you need a different take on arrays, read through this alternative article on arrays by zetcode.
- Arrays also allow for set operations. Don’t worry about the simple bookshelf example. Classes and Rails will be covered later!