# Why am I getting this string error?

**URL:** <https://www.ruby-forum.com/t/why-am-i-getting-this-string-error/75119>\
**Category:** Ruby\
**Created:** [November 7, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/why-am-i-getting-this-string-error/75119 "2006-11-07T16:54:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [November 7, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/why-am-i-getting-this-string-error/75119/1 "2006-11-07T16:54:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [November 7, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/why-am-i-getting-this-string-error/75119/2 "2006-11-07T17:16:27Z")

</div>

Peter B. 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.

> 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
```

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [November 7, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/why-am-i-getting-this-string-error/75119/3 "2006-11-07T19:41:53Z")

</div>

…  
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.
