DEV Community

Cover image for Seeking Ruby Array Methods...
Rachel Stark
Rachel Stark

Posted on

Seeking Ruby Array Methods...

Code challenges may keep you awake, but the outcome depends on the effort you make. Look to Ruby and you just might find a shortcut of some kind. Listed below are the methods you seek to make your code short and sweet.


Here you'll find a quick introduction to a few of the most used array methods in Ruby. Open up IRB in your terminal to test some of these array methods below with me and create some of your own.


shuffle method card looks like tarot card

The .shuffle method in Ruby returns a new array with all the elements of the original array.. shuffled.

method_card = [uniq, sample, rotate, reverse]
method_card.shuffle
# => [“reverse”, “sample”, “uniq”, “rotate”] 

lucky_number = [13, 5, 3, 8, 15]
lucky_number.shuffle
# => [5, 15, 3, 8, 13]
Enter fullscreen mode Exit fullscreen mode


sample method card looks like tarot card

The .sample method returns a random element or an array of random elements from the original array. Elements will not be repeated unless the original array contained duplicate elements.

fortune = ["It will come to pass", "Error 404 Fortune not found", "Not this year", "I will not tell"] 

fortune.sample
# => "I will not tell" 
fortune.sample
# => "Error 404 Fortune not found
fortune.sample(2)
# => ["Not this year", "I will not tell"] 
fortune.sample(3)
# => ["It will come to pass", "Error 404 Fortune not found", "Not this year"] 

Enter fullscreen mode Exit fullscreen mode


reverse method card looks like tarot card

The .reverse method returns a new array that contains the original array's elements in reverse order. You can also check out .reverse_each for more options.

fortune_teller = ["Madame Ruby", "Great React", "Code Seeker"] 
tellers = fortune_teller.reverse  
# => ["Code Seeker", "Great React", "Madame Ruby"] 

lucky_number = [13, 5, 3, 8, 15]
my_numbers = lucky_number.reverse
# => [15, 8, 3, 5, 13] 
Enter fullscreen mode Exit fullscreen mode


sort method card looks like tarot card

The .sort method returns a new array with the elements from the original array sorted. It does so by using the <=> operator to compare elements in the array. It's also referred to as the "spaceship operator", you can learn more about this operator. The .sort method will sort numbers from least to greatest and strings in alphabetic order. You can also chain on the .reverse method to get your new array in descending order.

lucky_number = [13, 5, 3, 8, 15] 
num1 = lucky_number.sort          
# => [3, 5, 8, 13, 15] 
num2 = lucky_number.sort.reverse  
# => [15, 13, 8, 5, 3] 

answer = ["yes", "no", "maybe", "doubtful"] 
a1 = answer.sort   
# => ["doubtful", "maybe", "no", "yes"] 
a2 = answer.sort.reverse 
# => ["yes", "no", "maybe", "doubtful"] 
Enter fullscreen mode Exit fullscreen mode

If you want to sort an array that contains something other than numbers or strings, then you would need to use .sort_by. Learn more


uniq method card looks like tarot card

The .uniq method comes in handy when working with lists of data that have some duplicate elements. You could filter out duplicate data by iterating through the array but why do that when you can simply use .uniq. The .uniq Ruby method is a non destructive method that returns a new array after removing duplicate elements from the original array.

lucky_number = [6, 13, 5, 3, 13, 8, 6]
num1 = lucky_number.uniq    
# => [6, 13, 5, 3, 8] 

# sort and discard duplicates
num2 = lucky_number.sort.uniq
# => [3, 5, 6, 8, 13] 

# sort by descending and discard duplicates
num3 = number.sort.reverse.uniq
# => [13, 8, 6, 5, 3] 
Enter fullscreen mode Exit fullscreen mode

Also note, that if you are merging data and want to avoid duplicates, you can use the | pipe operator which will merge arrays and discard any duplicates.

number_charm = [6, 13, 5, 3, 13, 8, 6]
number_choice = [4, 13, 3, 15, 18] 
combine = number_charm | number_choice
# => [6, 13, 5, 3, 8, 4, 15, 18] 

# using sort to organize
combine.sort
# => [3, 4, 5, 6, 8, 13, 15, 18]
# no duplicates from merge
Enter fullscreen mode Exit fullscreen mode

Whenever you find yourself using this method often, you may consider exploring Ruby sets. With Ruby sets, all items in a set are guaranteed to be unique. You can learn about the difference between sets and arrays. If you want to change the original array, you will need to use .uniq! which is destructive. More details on that coming up.


rotate method card looks like tarot card

The .rotate method returns a new array that rotates the elements of the original array so that the count comes first. You can also use negative numbers to rotate in the opposite direction. -1 being the last element of the array.

answer = ["yes", "no", "maybe", "doubtful"] 
a1 = answer.rotate     # => ["no", "maybe", "doubtful", "yes"] 
a2 = answer.rotate(2)  # => ["maybe", "doubtful", "yes", "no"] 
a3 = answer.rotate(3)  # => ["doubtful", "yes", "no", "maybe"] 
a4 = answer.rotate(-1) # => ["doubtful", "yes", "no", "maybe"] 
a5 = answer.rotate(-2) # => ["maybe", "doubtful", "yes", "no"] 

Enter fullscreen mode Exit fullscreen mode


bang method card looks like tarot card

When using the non destructive methods above you are preserving the original data. By adding an exclamation point ! to the end of these methods, you are creating a bang method. Bang methods must be used with caution because they are destructive, meaning they will mutate the data the method is called on.

Learn more Ruby array methods...

Ruby cards footer that says good luck

You can connect with me on LinkedIn & GitHub

Resources: Ruby-Doc.org, educative, Canva

Top comments (0)