Park and others!.....Possible memory bug in Ruby? I'm stumped!

All-

Thanks a million, everyone, for your comments in this thread.

One follow up:

Recall that in the code I asked about (included in thread below), one of the errors occured at line 15, where I write stuff to a new file. This line reads:

File.new(“./output/”+field[0],“w+”).write(content2)

Because an error occured here in one test, it appears that this line, like those before it, may also create an OS file. If this is true, is there way to rewrite THIS line to force a close the file during or just after the line executes? Or, perhaps immediate garbage collection occurs with this
command, so I needn’t worry.

Thanks again!

-Kurt Euler

···

-----Original Message-----
From: Park Heesob [mailto:phasis@kornet.net]
Sent: Sunday, June 09, 2002 1:38 AM
To: ruby-talk@ruby-lang.org
Subject: Re: Possible memory bug in Ruby? I’m stumped!

Hi,

“Kurt Euler” keuler@portal.com wrote in message
news:C47CCC6238EFD4119C5200508B95A100074AF63B@cup1ex1.portal.com

All-

The code as written after the dashed line way below (with file name t7.rb)
correctly inserts the contents of file “intro_subtemplate.txt” into a
template file during each iteration, resulting in the creation of some 189
identical but differently named files in directory “./output”. (There are
189
filenames in the 189 rows in file “packages.txt”.)

t7.rb:6:in ‘initialize’: Invalid argument -
“./input/intro_subtemplate.txt” (Errno::EINVAL)
from t7.rb:6:in ‘new’
from t7.rb:6
from t7.rb:2in ‘foreach’
from t7.rb:2

t7.rb:15:in ‘initialize’: Invalid argument -
“./output/SOL_BroadbandManager_6.2_SP1” (Errno::EINVAL)
from t7.rb:15:in ‘new’
from t7.rb:15
from t7.rb:2in ‘foreach’
from t7.rb:2

These error messages caused by too many open files.

Thanks in advance!!!

-Kurt Euler
(I’m using Ruby 1.67)

File t7.rb:

content = File.new(“./input/main_readme_template.txt”).read
IO.foreach(“./input/packages.txt”) { |x|
field = x.chop.split(‘:’)
content2 = content

inserta = File.new(“./input/intro_subtemplate.txt”).read
content2.gsub!(/<intro_subtemplate.txt>/, inserta)

#insertb = File.new(“./input/install_unix_subtemplate.txt”).read
#content2.gsub!(/<install_unix_subtemplate.txt>/, insertb)

#insertc =
File.new(“./input/files_changed_sp_num-1_subtemplate.txt”).read
#content2.gsub!(/<files_changed_sp_num-1_subtemplate.txt">/, insertc)

File.new(“./output/”+field[0],“w+”).write(content2)
puts field[0]
}

Following three lines should be placed before IO.foeach loop.

inserta = File.new(“./input/intro_subtemplate.txt”).read
insertb = File.new(“./input/install_unix_subtemplate.txt”).read
insertc = File.new(“./input/files_changed_sp_num-1_subtemplate.txt”).read

Or

inserta = File.new(“./input/intro_subtemplate.txt”).read

should be

f = File.new(“./input/intro_subtemplate.txt”)
inserta = f.read
f.close

Park Heesob.

Kurt,

Not sure I understand all your constraints, since I haven’t
closely followed this thread.

But be aware that ‘open’ is essentially the same as ‘new’ except
that it can take a block. If it’s given a block, the file will
be closed after the block is executed.

File.open(“./output/#{field[0]}”, “w+”) { write(content2) }

file is now closed

Does this help any?

By the way, my change to the file name is irrelevant… I just
wanted to illustrate a potentially clearer way of doing that
append.

Hal Fulton

···

----- Original Message -----
From: “Kurt Euler” keuler@portal.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, June 09, 2002 10:11 PM
Subject: RE: Park and others!..Possible memory bug in Ruby? I’m stumped!

File.new(“./output/”+field[0],“w+”).write(content2)

Because an error occured here in one test, it appears that this
line, like those before it, may also create an OS file. If this
is true, is there way to rewrite THIS line to force a close the
file during or just after the line executes? Or, perhaps
immediate garbage collection occurs with this
command, so I needn’t worry.

In article 006001c2102e$5a7a4de0$0300a8c0@austin.rr.com, Hal E. Fulton
wrote:
[…]

But be aware that ‘open’ is essentially the same as ‘new’ except
that it can take a block. If it’s given a block, the file will
be closed after the block is executed.

File.open(“./output/#{field[0]}”, “w+”) { write(content2) }

file is now closed

You mean
File.open(“./output/#{field[0]}”, “w+”) { |file| file.write(content2) }

Also, the leading “./” is unnecessary.

Right on both counts.

Hal Fulton

···

----- Original Message -----
From: “Tim Sutherland” timsuth@ihug.co.nz
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, June 10, 2002 3:19 AM
Subject: Re: Park and others!..Possible memory bug in Ruby? I’m stumped!