Seeking Ruby Array Methods...
Tags: ruby, beginners, codenewbie
Published: 2022-09-19 22:03:49

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](https://www.digitalocean.com/community/tutorials/how-to-use-irb-to-explore-ruby) in your terminal to test some of these array methods below with me and create some of your own.  

</br>
![shuffle method card looks like tarot card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqa5ajyee3qtpgspxz12.png)

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

```ruby
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]
```

</br>
![sample method card looks like tarot card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcg4cd190qc8a2nz3amm.png)

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.

```ruby
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"] 

```

</br>
![reverse method card looks like tarot card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lln0f9fy59a8kgc3d213.png)

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.](https://ruby-doc.org/core-3.1.0/Array.html#method-i-reverse_each
) 

```ruby
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] 
```

</br>
![sort method card looks like tarot card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wam6dfbdkw8myghxwfa8.png)

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.](https://ruby-doc.org/core-2.5.0/Comparable.html) 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. 

```ruby
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"] 
```
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](https://ruby-doc.org/core-3.1.0/Array.html#method-i-sort_by-21
)

</br>
![uniq method card looks like tarot card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvrbtpotvjnm9h6ifk9u.png)

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. 

```ruby
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] 
```
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. 

```ruby
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
```

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](https://www.rubyguides.com/2018/08/ruby-set-class/)  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.  

</br>
![rotate method card looks like tarot card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wyu9evlrwu3wytmq00gf.png)


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.

```ruby
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"] 

```
</br>
![bang method card looks like tarot card](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwftgqlnk2lx76dnhdt0.png)

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...](https://ruby-doc.org/core-3.1.0/Array.html)
</br>
![Ruby cards footer that says good luck](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzswa85i2j1g6mt8a4ub.png)




**You can connect with me on** [LinkedIn](https://www.linkedin.com/in/rachelsmithstark/) **&** [GitHub](https://github.com/rstarksmith)

Resources: [Ruby-Doc.org] (Ruby-Doc.org), [educative](https://www.educative.io/blog/ruby-array-methods), [Canva] (https://www.canva.com/)