As basically stated in the question title, is there a method on Ruby strings that is the equivalent to String#Scan but instead of returning just a list of each match, it would r
You could easily build your own by exploiting MatchData#end and the pos
parameter of String#match. Something like this:
def matches(s, re)
start_at = 0
matches = [ ]
while(m = s.match(re, start_at))
matches.push(m)
start_at = m.end(0)
end
matches
end
And then:
>> matches("foo _bar_ _baz_ hashbang", /_[^_]+_/)
=> [#, #]
>> matches("_a_b_c_", /_[^_]+_/)
=> [#, #]
>> matches("_a_b_c_", /_([^_]+)_/)
=> [#, #]
>> matches("pancakes", /_[^_]+_/)
=> []
You could monkey patch that into String if you really wanted to.