As we know that ruby contains a lot of built in modules, in which one of this is the Enumerable module. So before preceddingon what GREP is and How it works, first lets study a little info about Enumerable module.
The Enumerable module provide us a lot of traversal and searching methods which also gives us the ability to do sorting.
This module basically provides us simpler methods to manipulate collections with the help of searching, traversing and sorting.
The class provides the each method which yields all the successive members of the collection.
Enumerable module is included in the class as follows:
First of all GREP is a powerful UNIX command.
GREP is a fantastic and a very useful method which comes along with the enumerable module in Ruby.
Using GREP is very handy and useful when it comes to manipulate collections instead of using a map or select methods.
Along with, Grep is very useful in customizing the filtering, lets see how we can customize the filters. Firstly, put the categorization logic as case equal("===") in a class once it is done after that the instance of that class will be easily filtered out from an Enumerable object using GREP method.
In the ruby methods of enumerable, we use “select” method for filtering and use “map” method for bulk transforming, but grep act as a combination of both these methods.
How does GREP work?
To filter the elements in an enumerable object most of the time Select method is usedand it is done by passing a block in it, lets see an example how it is done: