On Apr 22, 12:32 pm, David Kahn <[email protected]> wrote: > Just curious if there is a more elegant way to set a variable if it happens > to not exist yet. I often find I am doing somthing like the following: > > regex = '' > 10.times { regex += '.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*,.*\n' } >
A mostly unrelated note - if you're really matching against a pattern like this, you may want to consider changing the .* bits to [^,]* to keep the backtracking under control. I put together a short set of benchmarks: https://gist.github.com/938732 Here's the results on my system (2010 13" MBP) (1000 iterations, except regex3): user system total real regex1: 2.490000 0.000000 2.490000 ( 2.590084) regex2: 0.050000 0.000000 0.050000 ( 0.048411) regex3 (/100): 25.600000 0.060000 25.660000 ( 26.839205) regex4: 0.080000 0.000000 0.080000 ( 0.090349) regex3 is the same .* pattern as regex1 but with 20 fields instead of 10 - note that in order to benchmark it in a reasonable time I cut the number of iterations from 1000 to 10 - in other words, regex3 would have taken 2560 seconds to complete the whole benchmark. One additional note - make sure you're not using *any* of these patterns with the Regexp::MULTILINE modifier, which makes '.' match newlines as well. I let a single instance matching regex1 run for about 10 minutes before giving up. Here's an article on this phenomenon ('catastrophic backtracking') with more detail: http://www.regular-expressions.info/catastrophic.html Hope this helps! --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

