2 ms·
The Ruby code is: IO.foreach('logs1.txt') {|x| puts x if /\b\w{15}\b/.match? x }
by cutler 8y ago
The Ruby code is:
IO.foreach('logs1.txt') {|x| puts x if /\b\w{15}\b/.match? x }
- yxhuvud 8y agoWhat happens if you lift the regexp out of the block and reuse the same regexp instance?
- cutler 8y agoTried: regex = Regexp.new '\b\w{15}\b' IO.foreach('logs1.txt') {|x| puts x if regex.match? x } ... but performance and RAM usage were identical.
- JohnBooty 8y agoRuby's smart enough to freeze regexen by default. I think that landed around 2.0/2.1/2.2 pry(main)> 3.times { puts /i_am_a_reg_ex/.object_id } 70131978677720 70131978677720 70131978677720