[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why am I getting this string error?

Peter Bailey

11/7/2006 3:55:00 PM

Can someone explain to me why I'm getting the error below for this part
of a script? It doesn't seem to like line 24, the scan line, but, I've
got this same code being used for other needs and it doesn't complain at
all there.

Thanks,
Peter


...
20 Dir.glob("*.ps").each do |psfile|
21 $filetime = File.ctime(psfile)
22 $filetime = $filetime.to_s.gsub!(/ -.*$/, "")
23 file_contents = File.read(psfile)
24 file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do
25 totalpages = $1
26 if (totalpages.to_i % 2) !=0 then
27 totalpages = totalpages.to_i + 1
28 file_contents << "\%\%Blank page for Asura.\n\%\%Page:
29 #{totalpages.to_i}\nshowpage\n"
30 File.open(psfile, "w") { |f| f.print file_contents }
31 FileUtils.touch(psfile)
32 end
...

getting this error:

>rubyw indexpagecounts_6x9.rbw
indexpagecounts_6x9.rbw:24:in `scan': string modified (RuntimeError)
from indexpagecounts_6x9.rbw:24
from indexpagecounts_6x9.rbw:20:in `each'
from indexpagecounts_6x9.rbw:20
>Exit code: 1

--
Posted via http://www.ruby-....

5 Answers

Vincent Fourmond

11/7/2006 4:15:00 PM

0

Peter Bailey wrote:
> Can someone explain to me why I'm getting the error below for this part
> of a script? It doesn't seem to like line 24, the scan line, but, I've
> got this same code being used for other needs and it doesn't complain at
> all there.

> 23 file_contents = File.read(psfile)
> 24 file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do
> 28 file_contents << "\%\%Blank page for Asura.\n\%\%Page:
> 32 end
> ...
>
> getting this error:
>
>> rubyw indexpagecounts_6x9.rbw
> indexpagecounts_6x9.rbw:24:in `scan': string modified (RuntimeError)
> from indexpagecounts_6x9.rbw:24
> from indexpagecounts_6x9.rbw:20:in `each'
> from indexpagecounts_6x9.rbw:20
>> Exit code: 1

You're modifying file_contents during scan, so scan gets confused.
from what I see, you'd better use gsub! with a block, since, if I
understood your point, you are basically inserting text where you detect
something.

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Peter Bailey

11/7/2006 6:42:00 PM

0

...
20 Dir.glob("*.ps").each do |psfile|
21 $filetime = File.ctime(psfile)
22 $filetime = $filetime.to_s.gsub!(/ -.*$/, "")
23 file_contents = File.read(psfile)
24 file_contents.scan(/\%\%Pages: (\d{1,5})[ ]+\n/) do
25 totalpages = $1
26 if (totalpages.to_i % 2) !=0 then
27 totalpages = totalpages.to_i + 1
28 file_contents. << "\%\%Blank page for Asura.\n\%\%Page:
29 #{totalpages.to_i}\nshowpage\n"
30 File.open(psfile, "w") { |f| f.print file_contents }
31 FileUtils.touch(psfile)
32 end
...

You're modifying file_contents during scan, so scan gets confused.
from what I see, you'd better use gsub! with a block, since, if I
understood your point, you are basically inserting text where you detect
something.

Vince

Thanks, Vince. I see what you mean, and, I did change it to use a gsub!.
I've always thought of gsub as a changer for little things, not huge
files like this, but, it works fine. Thanks again.

--
Posted via http://www.ruby-....

William Brower

11/5/2010 12:03:00 AM

0

On Nov 4, 7:54 pm, Del <delmail...@gmail.com> wrote:
> On Nov 4, 7:46 pm, "Lloyd Olson" <l...@ssbilliards.com> wrote:
>
> > The one on the right, he looks hungry.  LTG :)
>
> > "Del" <delmail...@gmail.com> wrote in message
>
> >news:06d49376-7ef7-464d-bdcb-3581ca9fd52c@26g2000yqv.googlegroups.com...
>
> > > Just playing here,,, Pick the one to watch out for ! .
>
> > >http://usergallery.myhomegameroom.com/gallery/album1276/DSC031... Hide quoted text -
>
> > - Show quoted text -
>
> Lloyd,
> If you look at the 2nd picture you will see the food pan's, That male
> on the right spends all day flipping those pan's right over the fence
> & into the bush's
> with his nose & I have to hunt for them everyday  =) .
>
> Bill was correct though, The lil bych in the middle is the one to
> watch & she's still a puppy  =)
>
> Pin-Del,

I would watch out for all three.
I would not want to get on the bad side of any of them.
The smaller one in the middle looks strong as hell too.

Bill

William Brower

11/5/2010 12:13:00 AM

0


Del, If you can train them to do this: http://lighthousepasco.files.wordpress.com/2010/05/dog...
It would save you on some clean up time.

Bill

Lloyd Olson

11/5/2010 12:20:00 AM

0

Sure Bill, put Del's wife out of work. LTG :)

"William Brower" <jerkymanbrower1@gmail.com> wrote in message
news:d9b485ae-3aa2-4881-b777-34d69c38db75@j25g2000yqa.googlegroups.com...
>
> Del, If you can train them to do this:
> http://lighthousepasco.files.wordpress.com/2010/05/dog...
> It would save you on some clean up time.
>
> Bill