Issue #15446 has been updated by shevegen (Robert A. Heiler).


The suggested idea by Cary seems fine to me. We have to ask
matz what he thinks about the proposed idea + name choice and
functionality.

I would suggest, however had, to, if necessary, deprecate at
a later time or decouple it from the suggestion here for now.

Reason being is mostly that deprecation (and then removing
functionality) is a little bit different to the proposal of
adding a new functionality (e. g. #matches or any other name
to class String). I think the step of deprecation could be
done at a later step or in another proposal. (I don't know
if anyone depends on producing an enumerator by gsub, but
in my opinion it would be just simpler to bypass that
question for now, and only focus on the suggested method 
addition Cary proposed.)

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

* 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>