DEV Community

Terry Threatt
Terry Threatt

Posted on

Ruby Array Cheatsheet of the methods you need to know.

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 = []
Enter fullscreen mode Exit fullscreen mode

Creating an Array with data

fruits = ["Apple", "Orange", "Grapes"]
Enter fullscreen mode Exit fullscreen mode

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) 
Enter fullscreen mode Exit fullscreen mode

Returning an element by position

Array#first - This will return the first element in the array index
cars = ["Tesla", "Ford", "Kia"]
cars.first #=> Tesla
Enter fullscreen mode Exit fullscreen mode
Array#last - This will return the last element in the array index
artists = ["Beyonce", "Madonna", "Drake"] 
artists.last #=> Drake 
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode
Array#shovel - It uses the shovel symbol "<<" to add elements
phones = ["iPhone", "Pixel"]
phones << ("Galaxy S") #=> phones = ["iPhone", "Pixel", "Galaxy S"]
Enter fullscreen mode Exit fullscreen mode
Array#unshift - Adds an element at the front of the array
phones = ["iPhone", "Pixel"]
phones.unshift("Galaxy S") #=> phones = ["Galaxy S", "iPhone", "Pixel"]
Enter fullscreen mode Exit fullscreen mode
Array#insert - Adds an element at index position
phones = ["iPhone", "Pixel"]
phones.insert(1, "Galaxy S") #=> phones = ["iPhone", "Galaxy S", "Pixel"]
Enter fullscreen mode Exit fullscreen mode

Removing elements

Array#pop - Removes an element at the end of array
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.pop #=> sneakers = ["Nike", "Adidas"]
Enter fullscreen mode Exit fullscreen mode
Array#shift - Removes an element at the front of the array
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.shift #=> sneakers = ["Adidas", "Puma"]
Enter fullscreen mode Exit fullscreen mode
Array#delete - Deletes an element by element name
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.delete("Nike") #=> sneakers = ["Adidas", "Puma"]
Enter fullscreen mode Exit fullscreen mode
Array#clear - Removes every element from the array
sneakers = ["Nike", "Adidas", "Puma"]
sneakers.clear #=> sneakers = []
Enter fullscreen mode Exit fullscreen mode

Sorting elements

Array#sort - Sorts elements from least to greatest
numbers = [2, 1, 4, 3]
numbers.sort #=> numbers = [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode
Array#reverse - Sorts elements from greatest to least
numbers = [2, 1, 4, 3]
numbers.reverse #=> numbers = [4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

More useful array methods

Array#length - Returns the number of elements in the array
dogs = ["boxer", "pug", "bulldog", "bulldog"]
dogs.length #=> 4
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode
Array#uniq - This removes duplicates from an array
dogs = ["boxer", "pug", "bulldog", "bulldog"] 
dogs.uniq #=> dogs = ["boxer", "pug", "bulldog"] 
Enter fullscreen mode Exit fullscreen mode
Array#empty? - Checks if the array is empty
dogs = ["boxer", "pug", "bulldog", "bulldog"] 
cats = []
dogs.empty? #=> false (always returns a boolean)
cats.empty? #=> true 
Enter fullscreen mode Exit fullscreen mode
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"]
Enter fullscreen mode Exit fullscreen mode
Array#select - Checks for the multiple instances of the element
dogs = ["boxer", "pug", "bulldog", "bulldog"]
dogs.select {|dog| dog == "bulldog"} #=> dogs = ["bulldog", "bulldog"]
Enter fullscreen mode Exit fullscreen mode
Array#each - Iterates through each element in array
names = ["Jack", "Jill"]
names.each {|name| puts "Hi, #{name}"}
#=> Hi, Jack 
#=> Hi, Jill
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
elhalvers profile image
Eric Halverson

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]

: )