From: Robert Klemme Date: 2012-02-03T19:42:44+09:00 Subject: Re: Basic Ruby performance On Fri, Feb 3, 2012 at 3:21 AM, Dmitry Nikiforov wrote: > Ryan Davis wrote in post #1043813: > >> IMHO re.match is just as useless as Regexp.new, Array.new, and Hash.new >> (assuming no args/blocks passed). They're throwbacks to java devs and >> serve no purpose but to make things more verbose. In this specific case, >> there are tangible reasons to use =~ over #match. > > The reason I tried to use Regexp.new is because I figured it would > pre-compile the regexp - the way "qr/ test /" in Perl would do, so that > it doesn't have to re-compile it on every iteration. Not necessary in Ruby: regexp literals are treated specially and are not recompiled. Usually it's faster to do io.each do |line| if line =~ /foo/ end end than rx = /foo/ io.each do |line| if line =~ rx end end If there is dynamic content, use /o: input = gets io.each do |line| if line =~ /foo:#{input}/o end end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/