2018-10-04 13:47:55 +00:00
---
title: Common Array Methods
---
## Common Array Methods
2018-12-13 11:27:31 +00:00
Arrays are a core foundation of programming in Ruby and most languages. Arrays are so common that it is beneficial to know, and even memorize, some of their most commonly used methods. If you want to know more about Ruby Arrays, we have [an article about them ](https://guide.freecodecamp.org/ruby/ruby-arrays ).
2018-10-04 13:47:55 +00:00
For the purpose of this guide, our array will be as follows:
``` ruby
array = [0, 1, 2, 3, 4]
2019-03-21 10:43:27 +00:00
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
```
#### .length
2019-03-21 10:43:27 +00:00
The `.length` method tallies the number of elements in the array and returns the count:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.length
=> 5
```
2019-03-21 10:43:27 +00:00
This is also similar to `.count` and `.size` methods.
2018-10-15 19:47:27 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-15 19:47:27 +00:00
array.count
=> 5
2018-12-13 11:27:31 +00:00
2018-10-15 19:47:27 +00:00
array.size
=> 5
```
2018-10-04 13:47:55 +00:00
#### .first
2019-03-21 10:43:27 +00:00
The `.first` method returns the first element of the array, the element at index 0:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.first
=> 0
```
#### .last
2019-03-21 10:43:27 +00:00
The `.last` method returns the last element of the array:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.last
=> 4
```
#### .take
2019-03-21 10:43:27 +00:00
The `.take` method returns the first n elements of the array:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.take(3)
=> [0, 1, 2]
2019-03-21 10:43:27 +00:00
array
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
```
#### .drop
2019-03-21 10:43:27 +00:00
The `.drop` method returns the elements after n elements of the array:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.drop(3)
=> [3, 4]
2019-03-21 10:43:27 +00:00
array
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
```
#### array index
2018-12-13 11:27:31 +00:00
You can return a specific element in an array by accessing its index. If the index does not exist in the array, `nil` will be returned:
2018-10-04 13:47:55 +00:00
```ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array[2]
=> 2
array[5]
=> nil
```
#### .pop
2019-03-21 10:43:27 +00:00
The `.pop` method will permanently remove the last element of an array and return this element:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.pop
2019-03-21 10:43:27 +00:00
=> 4
array
2018-10-04 13:47:55 +00:00
=> [0, 1, 2, 3]
```
#### .shift
2019-03-21 10:43:27 +00:00
The `.shift` method will permanently remove the first element of an array and return this element:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.shift
2019-03-21 10:43:27 +00:00
=> 0
2018-10-04 13:47:55 +00:00
array
=> [1, 2, 3, 4]
```
#### .push
2019-03-21 10:43:27 +00:00
The `.push` method will add an element to the end of an array and return the array:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.push(99)
=> [0, 1, 2, 3, 4, 99]
2019-03-21 10:43:27 +00:00
array
=> [0, 1, 2, 3, 4, 99]
2018-10-04 13:47:55 +00:00
```
2018-12-13 11:27:31 +00:00
2018-10-04 13:47:55 +00:00
#### .unshift
2019-03-21 10:43:27 +00:00
The `.unshift` method adds an element to the beginning of an array and return the array:
2018-10-04 13:47:55 +00:00
2019-03-21 10:43:27 +00:00
``` ruby
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
array.unshift(99)
=> [99, 0, 1, 2, 3, 4]
array
=> [99, 0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
```
#### .delete
2019-03-21 10:43:27 +00:00
The `.delete` method removes a specified element from an array permanently and return the element:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
array.delete(3)
=> 3
array
=> [1, 2, 4, 5]
2018-10-04 13:47:55 +00:00
```
#### .delete_at
2019-03-21 10:43:27 +00:00
The `.delete_at` method permanently removes an element of an array at a specified index and return the element:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
array.delete_at(3)
=> 3
array
=> [1, 2, 3, 5]
2018-10-04 13:47:55 +00:00
```
#### .reverse
2019-03-21 10:43:27 +00:00
The `.reverse` method returns a new array that includes the elements of the original array but in reverse order:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
new_array = array.reverse
2018-10-04 13:47:55 +00:00
=> [4, 3, 2, 1, 0]
2019-03-21 10:43:27 +00:00
new_array
=> [4, 3, 2, 1, 0]
array
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
```
2018-12-13 11:27:31 +00:00
2018-10-04 13:47:55 +00:00
#### .select
2019-03-21 10:43:27 +00:00
The `.select` method iterates over an array and returns a new array that includes any items that return true to the expression provided:
2018-10-04 13:47:55 +00:00
``` ruby
array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2019-03-21 10:43:27 +00:00
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_array = array.select { |number| number > 4 }
2018-10-04 13:47:55 +00:00
=> [5, 6, 7, 8, 9, 10]
2019-03-21 10:43:27 +00:00
new_array
=> [5, 6, 7, 8, 9, 10]
array
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2018-10-04 13:47:55 +00:00
```
#### .include?
2019-03-21 10:43:27 +00:00
The `.include?` method checks to see if the argument given is included in the array and returns `true` if it is:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.include?(3)
=> true
2019-03-21 10:43:27 +00:00
array.include?(5)
=> false
2018-10-23 19:09:26 +00:00
```
2018-10-04 13:47:55 +00:00
#### .flatten
2019-03-21 10:43:27 +00:00
The `.flatten` method can be used to take an array that contains nested arrays and create a one-dimensional array:
2018-10-04 13:47:55 +00:00
``` ruby
array = [1, 2, [3, 4, 5], [6, 7]]
2019-03-21 10:43:27 +00:00
=> [1, 2, [3, 4, 5], [6, 7]]
new_array = array.flatten
=> [1, 2, 3, 4, 5, 6, 7]
new_array
2018-10-04 13:47:55 +00:00
=> [1, 2, 3, 4, 5, 6, 7]
2019-03-21 10:43:27 +00:00
array
=> [1, 2, [3, 4, 5], [6, 7]]
2018-10-04 13:47:55 +00:00
```
#### .join
2019-03-21 10:43:27 +00:00
The `.join` method returns a string of all the elements of the array separated by a separator parameter. If the separator parameter is nil, the method uses an empty string as a separator between strings:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
array.join
2019-03-21 10:43:27 +00:00
=> "01234"
2018-10-04 13:47:55 +00:00
array.join("*")
2019-03-21 10:43:27 +00:00
=> "0*1*2*3*4"
array
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
```
#### .each
2019-03-21 10:43:27 +00:00
The `.each` method iterates over each element of the array, allowing you to perform actions on them:
2018-10-04 13:47:55 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
array.each { |element| puts element }
2018-10-04 13:47:55 +00:00
=>
0
1
2
3
4
2019-03-21 10:43:27 +00:00
array
=> [0, 1, 2, 3, 4]
2018-10-04 13:47:55 +00:00
```
2018-10-11 23:00:38 +00:00
#### .map
2019-03-21 10:43:27 +00:00
The `.map` method is the same as the `.collect` method. The `.map` and `.collect` methods iterate over each element of the array, allowing you to perform actions on them. The `.map` and `.collect` methods differ from the `.each` method in that they return an array containing the transformed elements:
2018-10-11 23:00:38 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
new_array = array.map { |element| element * 2 }
=> [0, 2, 4, 6, 8]
new_array
=> [0, 2, 4, 6, 8]
array
=> [0, 1, 2, 3, 4]
2018-10-11 23:00:38 +00:00
```
#### .uniq
2019-03-21 10:43:27 +00:00
The `.uniq` method returns a copy of the array containing only unique elements. Any duplicate elements are removed from the array. The original array is not modified.
2018-10-11 23:00:38 +00:00
``` ruby
array = [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8]
2019-03-21 10:43:27 +00:00
=> [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8]
new_array = array.uniq
=> [1, 2, 3, 4, 5, 6, 7, 8]
new_array
2018-10-11 23:00:38 +00:00
=> [1, 2, 3, 4, 5, 6, 7, 8]
2019-03-21 10:43:27 +00:00
array
=> [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 6, 7, 8]
2018-10-11 23:00:38 +00:00
```
#### .concat
2019-03-21 10:43:27 +00:00
The `.concat` method appends the elements from an array to the original array. The `.concat` method can take in multiple arrays as an argument, which will in turn append multiple arrays to the original array:
2018-10-11 23:00:38 +00:00
``` ruby
array = [0, 1, 2, 3, 4]
2019-03-21 10:43:27 +00:00
=> [0, 1, 2, 3, 4]
2018-10-11 23:00:38 +00:00
array.concat([5, 6, 7], [8, 9, 10])
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2019-03-21 10:43:27 +00:00
array
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2018-10-11 23:00:38 +00:00
```
2018-12-20 12:53:56 +00:00
#### .clear
2019-03-21 10:43:27 +00:00
The `.clear` method will delete all the data in the array:
2018-12-20 12:53:56 +00:00
``` ruby
2019-03-21 10:43:27 +00:00
array = [0, 1, 2, 3, 4]
=> [0, 1, 2, 3, 4]
2018-12-20 12:53:56 +00:00
array.clear
=> []
2019-03-21 10:43:27 +00:00
array
=> []
2018-12-20 12:53:56 +00:00
```
2018-10-04 13:47:55 +00:00
## More Information
2018-10-11 23:00:38 +00:00
* [Ruby Array docs ](http://ruby-doc.org/core-2.5.1/Array.html )