Issue #15446 has been reported by CaryInVictoria (Cary Swoveland).

----------------------------------------
Feature #15446: Add a method `String#matches` to the Ruby core
https://bugs.ruby-lang.org/issues/15446

* Author: CaryInVictoria (Cary Swoveland)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`String#matches` would be an alias of the form "gsub(pattern) вк enumerator" of [String#gsub](http://ruby-doc.org/core-2.5.1/String.html#method-i-gsub). 

I frequently use this form of `gsub` instead of `scan` when chaining to Enumerable methods. That's because `gsub` returns an enumerator whereas `scan` returns a temporary array. This use of `gsub` can also be useful when the pattern contains capture groups, which is sometimes a complication when using `scan` (such as when a capture group is needed for back-referencing).

Here is a simple example of its use.

    str = "Tina was friends with Mary and Sue. Tina and Mary loved to party. Sue and Tina went bowling every Thursday."

    str.gsub(/\b(?:Tina|Mary|Sue)\b/).each_with_object(Hash.new(0)) { |p,h| h[p] += 1 }
       #=> {"Tina"=>3, "Mary"=>2, "Sue"=>2}  
 
The problem with using `gsub` in this way is that it is confusing to readers who are expecting character substitutions to be performed. I also believe that the name of this method (the "sub" in `gsub`) has resulted in the form of the method that returns an enumerator to be under-appreciated and under-used.

Again, I am proposing that an alias be provided for the form of `gsub` that returns an enumerator. I suggest `String#matches`, but the choice of name is secondary.


  



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>