From: ara.t.howard@... Date: 2006-09-28T02:59:50+09:00 Subject: Re: problem with ".scan" On Thu, 28 Sep 2006, Peter Bailey wrote: > unknown wrote: >> On Thu, 28 Sep 2006, Peter Bailey wrote: >> >>>> -a >>> >>> Sorry. It's a bit much. That's why I was holding back. Here's the whole >>> script. >> >> >> >>> Dir.glob("*.ps").each do |psfile| >>> file_contents = File.read(psfile) >>> file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do >>> totalpages = $1 >>> if (totalpages.to_i % 2) !=0 then >>> newtotalpages = totalpages.to_i + 1 >>> file_contents << "\%\%Blank page for Asura.\n\%\%Page: >> ^^ >> ^^ >> ^^ >> ^^ >> the modification is question >>> #{newtotalpages.to_i}\nshowpage\n" >>> File.open(psfile, "w") { |f| f.print file_contents } >>> FileUtils.touch(psfile) >>> end >> >> so, ruby is correct, you are modifying a string while in an in-progress >> scan >> block. easy-cheasy. >> >> kind regards. >> >> -a > > > Thanks. I ended the scan block before doing any file writing. That > seemed to do the trick. It still confuses me, though, because, this code > was borrowed from an existing script that I've been using for 6 months, > and, that part of it is just as you see it above. probably because totalpages is always 1 - it's never even - in your new script the number of pages is always 2 (or 0) i'm guessing, and so the bug is triggered. if i we're you i'd update the other script - it's a bug in waiting. regards. -a -- in order to be effective truth must penetrate like an arrow - and that is likely to hurt. -- wei wu wei