Using The Select, Each, Map, And Sort Methods In Ruby

Brian Smith
3 min readAug 6, 2021

--

Ruby is a useful language to learn. It has many built in methods that solve algorithms easily for you. You could spend days looking through the different methods in Ruby. I think the Select Method, Each Method, Map Method, and Sort Method are some of the most popular methods. Even with all of the built in methods on Ruby, you can create even more of your own custom methods.

Select Method

The Select Method filters through an array of objects and returns the matching elements. If you have a large array of numbers but only want specific numbers in that array that are greater than a certain number, the select method should be used. Using this method you can select through that array and return specific numbers. In the below example, I have an array of numbers. I call select on that array. I want to return the numbers greater than 50 so I used n > 50.

numbers = [4, 10, 20, 25, 61, 78, 99, 16, 52, 86]filtered_numbers = numbers.select {|n| n > 50}filtered_numbers = [61, 78, 99, 52, 86]

Each Method

The Each Method allows you to iterate through an array of items and print those items or manipulate them to print something. In the below example, I iterate through each element in the array using the Each Method.

I have an array of four names and set it equal to a variable. I iterate through each element in the array and then used puts to print something on the screen. In this case, I printed “Hi Diana!, Hi Kate!, Hi Bill!, and Hi Chris!”. If you are not familiar with interpolation ‘ #{} ’, I used interpolation here to put each element inside ‘ #{n} ’.

name = ["Diana", "Kate", "Bill", "Chris"]name.each {|n| puts "Hi #{n}!"}
Hi Diana!Hi Kate!Hi Bill!Hi Chris!

Map Method

The Map Method is another popular method to use in Ruby. The Map Method returns an array with the results of whatever is called upon each element. For example, if you have an array of numbers and want to multiply each number by another number and store it in an array, you could utilize the Map Method. In the below example, I took numbers then iterated through each number and multiplied each number by another number. In this case, I added a number equal to a variable called count, but you could also use the number 2 instead of mixing in a variable.

numbers = [5, 20, 15, 1, 9]count = 2new_numbers = numbers.map {|n| n * count}new_numbers = [10, 40, 30, 2, 18]

Sort Method

The Sort Method is another popular method used in Ruby. It simplifies sorting elements in an array, which is especially when you have large amount of data to sort. The Sort Method will return an array alphabetically, from A to Z or numerically, from 1 to 100. It is simple to use. For example, below all I had to do was call sort on an array. There are also some more advanced ways to use the Sort Method.

numbers = [6, 50, 45, 29, 92]sorted_numbers = numbers.sortsorted_numbers = [6, 29, 45, 50, 92]names = ["Frank", "Emily", "Conor", "Alexa"]sorted_names = names.sortsorted_names = ["Alexa", "Conor", "Emily", "Frank"]

There are so many more awesome methods out there to explore. I hope some of these explanations help you out and as always happy coding!

--

--