Hi,
The following code is supposed to print multiple regular expression
matches on a given string:
class Myregexp < Regexp
def each (str)
pos = 0
while ((pos<str.size) && (m = match(str[pos…str.size])))
yield m
pos += m.end(0)
end
end
end
Myregexp.new(/pattern/).each(“string”){|m| $stdout << m[0]}
It works. However, it takes a lot of time and memory when "string" is
big. In my case, with a 10Mb string, it takes 40Mb of RAM. The problem comes
from the str[pos…str.size] (I solved the problem for my situation with a
hack where I changed it for str[pos,300], and everything runs fast and with
low memory). Since Ruby uses garbage collection and strings are, I believe,
copy on write, why does this occur?
I’m using Pragmatic Programmers Ruby 1.66 with Windows XP.
Thanks,
Maurício