Advanced uses of ‘select’ in Ruby

Advanced use cases for the ‘select’ method

Banura Randika
3 min readNov 14, 2023

In the world of Ruby programming, the select method is more than just a basic tool for filtering arrays. It’s versatile and can do a lot more than you might think. While many use it for simple tasks, there are advanced ways to make it work for you. In this article, we’ll explore how to use select in Ruby to solve tricky problems, manipulate data, and make your code more readable. Whether you’re new to Ruby or a pro, understanding the full potential of ‘select’ can help you write better, more efficient code.

Given an array inside of the select block, it will evaluate whether the condition is true or false. What select would do is it will provide a new array which will only satisfy the condition. In the most usual case, the select method works as follows. We have an array of few numbers and I need to select odd numbers.

Select example #1

We can even reduce the above code further as shown below. In there, we have taken advantage of the to_proc method in Ruby. If you want to learn more about it please check an article written about it from here.

Select example #2

Few advanced use cases

Use of the ‘select!’ method

We can use the select! (bang methods) method for in-place array filtering. It will change the original array instead of giving out a new array like when using the select method.

Select example #3

Chaining ‘select’ with other methods

We can combine and chain select method with other Enumerable methods. A good example of this is using the with_index method with the select method as shown below. In the example, you can see that we are using the array value and the index of a value for the consideration of the select condition.

Select example #4

Complex ‘select’ conditions in the block

Let's say you want to have a complex set of conditions when selecting from an array. For example, you want to select an object where one of the fields is not nil and you want to check if the updated_at timestamp is more than 1 hour old. I only took two conditions for the ease of the example but you can take as much as you want. Nevertheless, if you can get all of these conditions into a single Ruby code line it would work as previously mentioned example. If you are not able to get those into a single line or it is not readable in a single line you would need to do something similar to the following example to get it to work.

Select example #5

First of all, did you know next can take parameter value? I did not know that until I faced this challenge. I thought it was only used in a loop where it allows you to skip one iteration. If you want to learn more about next check this article about it from here.

In the above example, it explains that you could not use the return keyword instead you need to use the next keyword with a true/false argument. It is getting the user’s full address and the condition checked in the next line is that if the address is blank using next(false) won’t select it. The second condition checked for the time of the updated_at field whether it is updated in the last hour or not. This will give the desired result for the select operation using our conditions mentioned above.

The opposite of the select method is the reject method. Above mentioned ideas are also applicable to the reject method. If you have any better ideas or suggestions regarding the discussed methods and concepts please comment below.

I will be bringing these kinds of exciting articles where I explore nuggets throughout the Ruby ecosystem. Follow me now for a thrilling ride through all things Ruby 💎🤗

Email: randika.banura@gmail.com
LinkedIn:
https://www.linkedin.com/in/randika-banura/
Github:
https://github.com/randikabanura

--

--

Banura Randika

Experienced Software Engineer with a demonstrated history of working in the software industry. Skilled in Ruby, Python, Java, Javascript, Go, and Linux.