Useful Ruby Array Methods for Beginners — TL;DR versions

Spencer Smith
6 min readJun 8, 2020

If anyone else’s self-teaching experience with programming has been similar to mine, one of their first encounters with self-doubt probably happened right around when they first started reading documentation. For someone brand new to programming, these pages of code written in efficient-but-still-confusing syntax can be pretty daunting. Some methods are just trying to get one item from an array while others are trying to transform all of the elements. Some methods are so obscure that you can hardly even imagine a scenario where you’d ever use it. Some methods are even aliases (guys, please just pick one).

My goal was to go through the Ruby documentation and pick out only the most necessary and regularly useful array methods. This really helped me gain a good understanding of what methods are capable of in general and how I can leverage the most common ones to make my life easier.

Accessing Elements

For when you just want to “look” at certain elements in an array without changing the original array

One element from anywhere in the array

.at (index as an argument)

This is basically the same thing as using brackets to access a certain element. Want to see what’s at index 3? You can do array.at(3) or array[3]. They’re the same thing.

.fetch (index you want to look for, thing that gets passed back if that index doesn’t exist)

This is the same thing as using .at, but the program will return something you define if the index doesn’t exist.

One element from the beginning or end of an array

.first

This will return the first element (index 0) of your array.

.last

This will return the last element (index -1) of your array.

Several elements from the beginning or end of an array

.take (the number of elements you want to take from the front of your array)

Want to know what the first five elements of your array are? Try array.take(5)

.take_while {|item| condition that will be true if you want that item}

For when you want to “take” elements from the beginning of your array until one of the elements doesn’t meet your condition. If you had an array of alphabet letter characters and wanted all of the letters up until the letter “g”, you could do…. alphabet.take_while {|letter| letter != “g”}

.drop (the number of elements you want to take from the back of your array)

The alter-ego of .take, but from the end of your array working backwards. Want to know the last seven elements of your array? array.drop(7)

.drop_while {|item| condition that will be true if you want that item}

The alter-ego of .take_while, but from the end of your array working backwards.

However many you want, from wherever you want

.slice (starting index, number of elements you want)

Similarly to .at, this is sometimes written with brackets. array.slice(3,5) and array[3,5] are the same thing. This is useful when you want to grab several elements that are all next to each other in an array.

Obtaining information about the array itself

For when you’re just looking to know a couple of things about your array

.length/.count /.size

These methods are aliases, meaning they do the same thing to an array. They let you know how many elements (or indexes) are inside of the array. The only difference is that .count doesn’t work on strings.

.empty?

Does this array have anything in it?

.include? (thing you’re looking for)

Is this thing you’re looking for somewhere in the array?

Removing or adding items from an array

For when you do or don’t want certain stuff in your array

When you want to add or remove items from the beginning or end of your array

.push (whatever you want to add to the end of your array)

I want to add this thing to the end of my array.

.pop

I want to remove the element at the end of my array and possibly keep it somewhere else.

.shift (whatever you want to add to the beginning of your array)

I want to add this thing to the front of my array.

.unshift

I want to remove the first thing at the front of my array and possibly keep it somewhere else.

When you want to add or remove an item somewhere specific in your array

.insert (index, thing you want to insert at the given index)

*important* : this method does not delete anything that was previously at this index. Instead, it moves elements over to make room for your new item(s). For example: [0, 1, 2, 3, 4, 5].insert(3, “tacos”) would result in [0, 1, 2, “tacos”, 3, 4, 5]

.delete_at (index where you want to delete something)

Whatever index you passed in, delete the item at that given index.

When you want to delete multiple instances of the same thing

.delete (item you want to delete)

This gets rid of all instances of whatever you passed in. For example: [“boots”, “cats”, “puppies”, “cats”, “boots”, “cats”].delete(“cats”) would result in [“boots”, “puppies”, “boots”]

.compact

This method removes all nil values from your array, keeping it nice and tidy.

.uniq

For when you don’t want any duplicates.

Iterating over every element of an array

For when you need to look at every element inside of an array

When you mostly just want to peek at the data

.each {|item| action you want to perform using each item}

Super generic, returns original array. Let’s say you want to go through an array of objects from the beginning and update each object’s mood to “happy.” You could do…… array.each {|person| person.mood = “happy”}

.reverse_each {|item| action you want to perform using each item}

Super generic, the same thing as .each but starting at the end of the array going backwards. Let’s say you want to go through an array of objects backwards and print out the names of each object. You could do……. array.reverse_each {|person| puts person.name}

When you’re looking to return a new, transformed version of the data

.map / .collect {|item| the way you would like to transform each item}

These two methods are aliases and do the same thing. This is used when you want to change every item in the array in a certain way, and then return the new copy of your array. Note that .map doesn’t change the original array, it returns a new array that meets your specifications. For example, if you want to multiply each integer in an array, you could do……

[1, 2, 3, 4, 5, 6, 7].map {|num| num * 3}

which would return…….

[3, 6, 9, 12, 15, 18, 21]

Selecting certain items from an array based on customized specifications

“I want certain items in this array, but only if they are…..(fill in the blank)”

Non-Destructive (doesn’t mess up the original array, but gives you a new one)

.select {|item| condition}

“I want all of the items in an array that meet my conditions”

In an array of strings, give me a new array of all the strings that have more than five letters:

array.select {|word| word.length > 5}

In an array of integers, give me a new array of all of the integers that are even:

array.select {|num| num % 2 == 0}

.reject {|item| condition}

The alter-ego of select. “I want all of the items in an array that DON’T meet my conditions”

In an array of strings, return only the words that DON’T start with the letter a:

array.reject {|word| word[0].upcase == “A”}

Destructive (changes the original array)

.keep_if {|item| condition}

Basically the same thing as .select, but destructive. “I want to go through this array and keep only the items that meet this condition”

In an array of people objects, keep only the ones named “Sam”:

array.keep_if {|person| person.name == “Sam”}

.delete_if {|item| condition}

The alter-ego to .keep_if and basically the same thing as .reject, but destructive. “I want to go through this array and delete all of the items in the original array that meet this condition”

In an array of booleans, delete all of the false values:

array.delete_if {|bool| bool == false}

Sign up to discover human stories that deepen your understanding of the world.

No responses yet

Write a response