Here is a beginner-friendly list of the more common array methods you will need to keep in your toolbox as a Ruby developer. Arrays are one of the most common data structures in programming. Consisting of a container for a collection of data of any type. It holds an index that starts at "0" and Ruby has several built-in methods that will make your life easier working with arrays in your projects.
Creating an empty Array
empty_array = []
Creating an Array with data
fruits = ["Apple", "Orange", "Grapes"]
Accessing elements
Array#index - Using the index of sports array to return element
sports = ["baseball", "football", "basketball"]
sports[1] #=> football (Reminder: Arrays are 0)
sports[0] #=> baseball (index of 0 == baseball)
Returning an element by position
Array#first - This will return the first element in the array index
cars = ["Tesla", "Ford", "Kia"]
cars.first #=> Tesla
Array#last - This will return the last element in the array index
artists = ["Beyonce", "Madonna", "Drake"]
artists.last #=> Drake
Modifying elements
Array#push - Pushes an element at the end of the array
phones = ["iPhone", "Pixel"]
phones.push("Galaxy S") #=> phones = ["iPhone", "Pixel", "Galaxy S"]
Array#shovel - It uses the shovel symbol "<<" to add elements
phones = ["iPhone", "Pixel"]
phones << ("Galaxy S") #=> phones = ["iPhone", "Pixel", "Galaxy S"]
Array#unshift - Adds an element at the front of the array
phones = ["iPhone", "Pixel"]
phones.unshift("Galaxy S") #=> phones = ["Galaxy S", "iPhone", "Pixel"]
Array#insert - Adds an element at index position
phones = ["iPhone", "Pixel"]
phones.insert(1, "Galaxy S") #=> phones = ["iPhone", "Galaxy S", "Pixel"]
Removing elements
Array#pop - Removes an element at the end of array
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.pop #=> sneakers = ["Nike", "Adidas"]
Array#shift - Removes an element at the front of the array
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.shift #=> sneakers = ["Adidas", "Puma"]
Array#delete - Deletes an element by element name
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.delete("Nike") #=> sneakers = ["Adidas", "Puma"]
Array#clear - Removes every element from the array
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.clear #=> sneakers = []
Sorting elements
Array#sort - Sorts elements from least to greatest
numbers = [2, 1, 4, 3]
numbers.sort #=> numbers = [1, 2, 3, 4]
Array#reverse - Sorts elements from greatest to least
numbers = [2, 1, 4, 3]
numbers.reverse #=> numbers = [4, 3, 2, 1]
More useful array methods
Array#length - Returns the number of elements in the array
dogs = ["boxer", "pug", "bulldog", "bulldog"]
dogs.length #=> 4
Array#include? - Checks if an array includes an element
dogs = ["boxer", "pug", "bulldog", "bulldog"]
dogs.include?("pug") #=> true (always returns a boolean)
dogs.include?("yorkie") #=> false
Array#uniq - This removes duplicates from an array
dogs = ["boxer", "pug", "bulldog", "bulldog"]
dogs.uniq #=> dogs = ["boxer", "pug", "bulldog"]
Array#empty? - Checks if the array is empty
dogs = ["boxer", "pug", "bulldog", "bulldog"]
cats = []
dogs.empty? #=> false (always returns a boolean)
cats.empty? #=> true
Array#detect - Checks for the first instance of the element in the array
dogs = ["boxer", "pug", "bulldog", "bulldog"]
dogs.detect {|dog| dog == "bulldog"} #=> dogs = ["bulldog"]
Array#select - Checks for the multiple instances of the element
dogs = ["boxer", "pug", "bulldog", "bulldog"]
dogs.select {|dog| dog == "bulldog"} #=> dogs = ["bulldog", "bulldog"]
Array#each - Iterates through each element in array
names = ["Jack", "Jill"]
names.each {|name| puts "Hi, #{name}"}
#=> Hi, Jack
#=> Hi, Jill
Practice
Get out into the wild and practice these methods on arrays in projects. These methods will be tools to solve plenty of Ruby programming problems. Arrays will come up in many languages as well so you could repurpose your knowledge of how to work with arrays. If this cheatsheet was helpful to you, please share this post with another developer.
Happy coding,
Terry Threatt
Top comments (1)
Hey Terry, Thanks so much! This is great! : )
One note: Array#reverse - does not sort elements from greatest to least. It reverses the current array order.
For example:
numbers = [2, 1, 4, 3]
numbers.reverse #=> numbers = [3, 4, 1, 2]
: )