--geqNt1NAoYRYy2Pr Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable > > Howdy. What's the best way to prevent infinite loops in Ruby? > > Don't write one. :) I wish I could... I'm using a recursive regexp to remove all <?tmpl.* ?> tags from a template and tags can include tags. > I guess I'd ask: what's the driver of the loop? Is it the test for > some_action, or is it the count, or both. If the count is incidental, > then I'd tuck it away inside the loop (although possibly making it > slightly shorter) In a nut shell, here's what I'm doing: tmpl = large_string tag_re = Regexp.new('\<\?tmpl\.([^\s\?]*)\s*(.*?)\s*\?\>') loop_count = 0 while md = tag_re.match(tmpl) if loop_count > @max_num_iteration raise(RuntimeError, "Too many iterations") else loop_count += 1 end ... tag = md.to_a[0] ... tmpl.gsub!(/#{Regexp.escape(tag)}/, val) ... end > while some_action and loop_count < 1000 I kinda like this, but want to raise an exception in the event that it loops on for a while. I think what I'll probably do is reimplement this as a range and test for the Exception after I've incremented over the range (that way I don't test whether to raise an exception on every iteration). Any thoughts are welcome. <promoter> Actually, let me take this opportunity to put in a shameless plug for the ruby-tmpl library that runs under mod_ruby. I've just released version 0.2 of this library and would love to get this out and running on more systems. The project is being hosted at SourceForge and can be checked out at: http://ruby-tmpl.sourceforge.net/ A few tid bits about it: 1) It does work and is stable to use. I eat my own dog food and am developing two sites with it right now. 2) It is primarily used for creating dynamic/templated pages, however I do anticipate having offline functionality completed by 0.3 so that pages can be compiled offline. 3) This is not a replacement of eRuby, it's only a templating library for sites that need to separate functionality/information from presentation. It is a module that works in parallel with mod_ruby. 4) There is an output filtering layer that I hope to expose and use more. Some of the things that I plan on doing real soon are add session support (using both URIs and cookies) and dictionaries support (keyword -> url mapping). 5) I'd like to think that things are well organized and documented, please prove me wrong (or die trying). Please feel free to join the -users and -devel list. Documentation about how/where is listed under the FAQs on the site. Enjoy! --Sean http://ruby-tmpl.sourceforge.net/ </promoter> -- Sean Chittenden --geqNt1NAoYRYy2Pr Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Comment: Sean Chittenden <sean / chittenden.org> iEYEARECAAYFAjtWJDoACgkQn09c7x7d+q16igCfVaPzepUe++z9JXR4Rt2CjFWY XEUAn3JtHuD8bmRe5hXRXzvWSv9I0/Ut W1 -----END PGP SIGNATURE----- --geqNt1NAoYRYy2Pr--