Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
To filter out the elements from the array in Ruby, there are two methods, select() and reject() can be used. select() will help us to select the elements that meet the specified condition, and reject() will help us to reject the elements that will meet the specified criteria. Both methods return a new array after completing the filtering.
In the article ‘Ruby Array Filter’, we will discuss why and how filtering can be done in Ruby Array and the filtering methods of filtering out the elements with specified conditions along with their implementations.
In this section of the article ‘Ruby Array Filter’, we will discuss why and how filtering can be done in the array in Ruby. Filtering is nothing but returning the elements of an array that satisfy the given condition or criteria. But why do we need filtering methods? Can’t we do it manually?
One of the main advantages of using filtering methods is to reduce the effort of the programmer. Here is an example to understand this more:
We want to filter out the elements of an array; if the elements are odd, we want them to be returned or filtered. Below is the code to do the same manually:
# Given an array of elements.
numbers = [1, 2, 3, 4, 5]
puts "Given Array: "
print numbers
# Create an odds array that will store the odd elements in this array.
odds = []
# An iteration to filter odd numbers.
for num in numbers
if num % 2 != 0
odds << num
end
end
# Print the elements of odd
puts "\nResult array with only odd elements"
print odds
You can also try this code with Online Ruby Compiler
In the above, we performed filtering manually where an iteration is done, and if an element is not a modulo of 2 means it is an odd element, it is stored in an ‘odds’ array. This manual task can be hectic sometimes. So now below is the code to do the same with the help of using a filter method called select():
# Given an array of elements.
numbers = [1, 2, 3, 4, 5]
puts "Given Array: "
print numbers
# The select() method is used to filter out the odd numbers.
odds = numbers.select { |number| number % 2 != 0 }
# Print the elements of odd.
puts "\nResult array with only odd elements"
print odds
You can also try this code with Online Ruby Compiler
In the above code, we used the select method to filter out the odd numbers; we will see the select method in the later sections. As we can see from the lines of code in both cases, using the filter method is definitely beneficial rather than doing code manually.
The output of the above codes:
Filtering Methods in Ruby
In this subsection of the article ‘Ruby Array Filter’, we will discuss some filtering methods by which you can filter out the array elements in Ruby. There are different ruby array filters depending on the problem in which these filters are being used. Here are as follows:
Select Method
The method select() in Ruby can be used to return the elements that satisfy the specified condition. This method returns a new array after filtering out the elements from the given array in Ruby.
Let’s say you are working with products API, where a large number of products are present. So there is no need to get all the product data. Instead, you can use the select() method to get some important products with the specified id, name, price, or something like that.
Here is the code for using the select method:
# Given an array of elements.
numbers = [10, 5, 3, 11, 50, 29, 83, 90]
puts "Given Array: "
print numbers
# The select() method is used to filter out the elements greater than 15.
result = numbers.select { |num| num > 15 }
# Print the elements of the result.
puts "\nResult array with only elements greater than 15"
print result
You can also try this code with Online Ruby Compiler
In the above code, an array of elements ‘numbers’ were given. The method select is used here by specifying the condition that will filter out the elements greater than 15, which returns the ‘result’ array. Lastly, the array ‘result’ is printed that will only contain elements greater than 15.
Time Complexity:
The select() method is used here to filter out the array elements that will iterate the array only once. So the overall time complexity is O(n), where n is the number of elements in the given array.
Space Complexity:
There is a given array ‘numbers’ and an array ‘result’ that is returned using the select() method. In the worst case, this ‘result’ array can be of ‘n’ size. So the total space complexity is O(2n), where n is the number of elements in the given array.
The output of the above code:
Reject Method
The method reject() in Ruby can be used to return the elements that don't satisfy the specified condition. The method reject() works the same as the select() but in the opposite manner, and the reject() method also returns a new array after filtering out the elements.
Imagine you working with the list of marks of the students. An exam is being conducted for which the students with marks less than 50 can not participate. Then the reject() method can be used by just passing the condition that the marks should be greater than 50.
Here is the code for using the reject method:
# Given an array of elements.
numbers = [10, 5, 3, 11, 50, 29, 83, 90]
puts "Given Array: "
print numbers
# The reject() method is used to filter out elements not greater than 15.
result = numbers.reject { |num| num > 15 }
# Print the elements of the result.
puts "\nResult array with only elements greater than 15"
print result
You can also try this code with Online Ruby Compiler
In the above code, an array of elements ‘numbers’ were given. The method reject is used here by specifying the condition and the elements that fail the condition of the elements greater than 15, which returns the ‘result’ array. Lastly, the array ‘result’ is printed that will only contain elements that are not greater than 15.
Time Complexity:
The reject() method is used here to filter out the array elements that will iterate the array only once. So the overall time complexity is O(n), where n is the number of elements in the given array.
Space Complexity:
There is a given array ‘numbers’ and an array ‘result’ that is returned using the reject() method. In the worst case, this ‘result’ array can be of ‘n’ size. So the total space complexity is O(2n), where n is the number of elements in the given array.
The output of the above code:
Find Method
The method find() in Ruby can be used to return the first element in the array that satisfy the specified condition. If there is an element in the array satisfying the condition, it will return that element else, nil will be returned.
Let’s say you are working with the list of passwords. You can imagine a list as an array containing strings (passwords). Now if you want to find one password that matches with the password inputted by the user. The find() method can be used as it will return the first password that will match with inputted password else nil will be returned.
Here is the code for using the find method:
# Given an array of elements.
numbers = [10, 5, 3, 11, 50, 29, 83, 90]
puts "Given Array: "
print numbers
# The find() method is used to filter out first element greater than 15.
result = numbers.find { |num| num > 15 }
# Print the elements of result.
puts "\nResult element greater than 15:"
print result
You can also try this code with Online Ruby Compiler
In the above code, an array of elements ‘numbers’ were given. The method find is used here by specifying the condition and the first element that meets the condition of the elements greater than 15, which returns the ‘result’ element. Lastly, the element ‘result’ is printed that will only contain the element greater than 15.
Time Complexity:
The find() method is used here to filter out the array elements that will iterate the array only once. Even though it will return only one element, and then the iteration will be stopped, in the worst case, the element can be at the last index of the array. So the overall time complexity is O(n), where n is the number of elements in the given array.
Space Complexity:
There are only one array ‘numbers’. So the total space complexity is O(n), where n is the number of elements in the given array.
The output of the above code:
In-place Select
In-place selection can also be made if you want to filter out the elements in place in the given array of elements that satisfy the specified condition. In-place selection is made using the method select!
If there is a list of usernames and you want to filter out the usernames that do not contain a user in their username. The in-place select method can be used, which will do this, and one of the advantages of using in-place select is that the filtering will be done in place.
Here is the code for filtering out the element in place:
# Given an array of elements.
numbers = [10, 5, 3, 11, 50, 29, 83, 90]
puts "Given Array: "
print numbers
# The select() method is used to filter out the elements greater than 15.
numbers.select! { |num| num > 15 }
# Print the elements of the numbers after filtering.
puts "\nNumbers array with only elements greater than 15"
print numbers
You can also try this code with Online Ruby Compiler
In the above code, an array of elements ‘numbers’ were given. The method select! is used here by specifying the condition, and the elements that meet the condition of the elements greater than 15 will be filtered out in place. No extra array will be returned. Lastly, the array ‘numbers’ is printed that will only contain elements greater than 15.
Time Complexity:
The in-place select (or select!) method is used here to filter out the array elements that will iterate the array only once. So the overall time complexity is O(n), where n is the number of elements in the given array.
Space Complexity:
There is only one array ‘numbers’, and no extra memory is taken as the filtering is done in place here. So the total space complexity is O(n), where n is the number of elements in the given array.
Filtering is nothing but returning the elements of an array that satisfy the given condition or criteria. There are two methods in Ruby, select() and reject(), which can be used to filter out the elements of the array.
What is the select() method in Ruby?
The method select() in Ruby can be used to return the elements that satisfy the specified condition. This method returns a new array after filtering out the elements from the given array in Ruby.
What is the reject() method in Ruby?
The method reject() in Ruby can be used to return the elements that don't satisfy the specified condition. The method reject() works the same as the select() but in the opposite manner, and the reject() method also returns a new array after filtering out the elements.
What does the find() method do in Ruby?
The method find() in Ruby can be used to return the first element in the array that satisfy the specified condition. If there is an element in the array satisfying the condition, it will return that element else, nil will be returned.
What does the in-place select() method do in Ruby?
In-place selection can also be made if you want to filter out the elements in place in the given array of elements that satisfy the specified condition. In-place selection is made using the select! method.
Conclusion
The filtering out of the array element can be done using various filtering methods in Ruby, such as select, reject, find, and in-place select methods. In the article ‘Ruby Array Filter’, we discussed what is filtering, why to use filtering methods, and various filtering methods in Ruby along with their implementations.
Here are more articles that are recommended to read: