0006 Grep

Yes, we're talking about Ruby, not the command-line utility!

The .grep method is the equivalent of the command-line utility. It allows you to filter an Enumerable using a pattern. There’s even the equivalent of the -v option to invert the match!

0006-grep_1.png

The pattern does not have to be a regex. The method used is === which, for example, allows the use of proc.

0006-grep_2.png

⚠️ The proc must be passed as a parameter. A block can still be used and will do the equivalent of a .map.

0006-grep_3.png

Link to Ruby documentation for grep.