Issue #6802 has been updated by prijutme4ty (Ilya Vorontsov).


Thank you for a solution! I always forgot about regexp global vars. Though I suggest that using a special method here is more clear. So what'd you say about String#each_match and Regexp#each_match
Yes, implementation is as simple as
class String
  def each_match(pat)
    scan(pat){ yield $~ }
  end
end

and similar for Regexp.


Eregon (Benoit Daloze) wrote:
> =begin
> You can use (({String#scan})) with the block form and (({$~})) (as well as other Regexp-related globals) for this:
> 
>     > text="x:1 y:12 ; x:33 y:2"
>     > text.scan(/x:(?<x>\d+) y:(?<y>\d+)/) { p [$~[:x],$~[:y]] }
>     ["1", "12"]
>     ["33", "2"]
> 
> Please check your Regexp and give an example of (({text})) next time.
> =end


----------------------------------------
Feature #6802: String#scan should have equivalent yielding MatchData
https://bugs.ruby-lang.org/issues/6802#change-28528

Author: prijutme4ty (Ilya Vorontsov)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


Ruby should have method to obtain not an array of arrays but of MatchData objects. It can help in obtaining named groups:

pattern = /x: (?<x>\d+) y:(?<y>\d+)/
polygon = []
text.scan_for_pattern(pattern){|m| polygon << Point.new(m[:x], m[:y]) }

Not to break existing code we need unique name. Ideas? May be #each_match


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