Helpful Built-In Ruby Array Methods

Yahjaira Vasquez
4 min readSep 8, 2020

Ruby has many built in functions that are save us from writing out long code and I am unsure about you, but I know I do not explore them enough. I have my set few that I use consistently, but today I wanted to mention some that are not necessarily commonly used geared towards arrays.

Append

Appending to arrays is very common, but typically people use ‘.push()’. There is nothing wrong with this, it gets the job done. But for those who like to minimize the amount of typing they do there is a simple append symbol (‘<<’) that can be used. On the left of the append symbol you have the array or variable array and to the right of the append symbol is what you would like to add the end of the array.

Set Intersection

Set intersection (‘&’) compares two arrays and returns a new array of the unique elements that occur in both array. There will be no repetitive numbers displayed in the new array returned. This method can save you lines of codes next time you are trying to find common points in two arrays. To implement you simply need to have two array, separated by the ampersand symbol (‘&’).

Difference

The array difference method is opposite of the set intersection. The array difference method returns a copy of the original array with the values that appeared in the argument array/s removed. The array that is return also does not provide only unique values, therefore there may be some repetition, if original array has values that are repetitive. You can also compare an array with more than one other array. Let’s say you want to compare three arrays. You simply pass two arrays as arguments to the difference method and the method will return the original array minus any values that appeared in the argument arrays.

Include?

The include method is definitely a part of my often used methods, but I thought it is worth the mention here. This method is helpful in determining is a certain value can be found inside of a given array. This method does return a boolean value, helping to determine if the array does or does not include the value. To set this up, all you need is an array, then pass in the value you want to check for as arguments to the include? method.

Permutation

Permutation will find all possible permutations of an array. It returns an enumerator instance, therefore you want to make sure you call ‘.to_a’ after your method to ensure an array is returned. If no argument is given, all of the possible permutations will be returned, but if you pass in a number as an argument to the method, an array of permutations will be return with the length of the number passed as the argument. This could definitely come in handy when taking technical interviews.

Product

Product is used to find all possible combinations between two or more arrays. This will return a new array of arrays. to set this up you call ‘.product()’ on an array, then pass in any number of arrays as an argument to the product method.

Uniq

Uniq is another method that I use quite often, but definitely is worth the mention here. Uniq cleans up your array to only show unique values, therefore there will be no repetitive values. It is very quick and easy to implement. Just call ‘.uniq’ after any array you would like to apply this method to.

Set Union

The last method I would like to mention here today is the set union (‘|’) method. This method return a new array, joining to arrays and only providing uniq values. This method is pretty straight forward, to implement you just need to array separated by the vertical bar (‘|’) symbol.

These are just a few of the many methods ruby has provided us with. I hope that this has inspired you to dive a little deeper to find some new methods you can use in your next project or coding challenge. Ruby has provided many methods to make our lives easier, we just need to do a little research to find a method for our needs. If you would like to do a little more research of your own or read more about the aforementioned methods, you can find all of the information here in the Ruby docs.

--

--

Yahjaira Vasquez

Seeking and spreading knowledge within the world of tech!