class Regexp

example usage of Regexp#global_match re = /(w+)/ words = [] re.global_match(“cat dog house”) do |m|

words.push(m[0])

end p words # [“cat”, “dog”, “house”]

Public Instance Methods

global_match(str, &proc) click to toggle source
# File lib/mgnu/common.rb, line 33
def global_match(str, &proc)
  retval = nil
  loop do
    res = str.sub(self) do |m|
      proc.call($~) # pass MatchData obj
      ''
    end
    break retval if res == str
    str = res
    retval ||= true
  end
end