11.7. Common methods that use regular expressions

The payoff for gaining facility with regular expressions in Ruby is the ability to use the methods that take regular expressions as arguments and do something with them.

To begin with, you can always use a match operation as a test in, say, a find or find_all operation on a collection. For example, to find all strings longer than 10 characters and containing at least 1 digit, from an array of strings called array, you can do this:

array.find_all {|e| e.size > 10 and /\d/.match(e) }

But a number of methods, mostly pertaining to strings, are based more directly on the use of regular expressions. We’ll look at several of them in this section.

11.7.1. String#scan

The scan method goes from left to ...

Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.