Hi,
In message "[ruby-talk:00409] open without variables and implicit closing"
on 99/07/01, Julian R Fondren <julian / cartotech.com> writes:
|If I use File.open without saving its return to a file, how can I or do I
|even need to close the file? That is: with
|``File.open('file', 'r').each{|l| print l}'' where all I'd otherwise want
|to is close the file, could I just not bother with this at all and know
|that the file had been closed?
The unreferenced file will be closed on next GC. So, you can forget
about it unless you require immediate close/flush of the file.
If you need immediate close, you can do either:
(a)
f = File.open('file', 'r')
begin
f.each{|l| print l}
ensure
f.close
end
(b)
File.open('file', 'r') do |f|
f.each{|l| print l}
end
(c)
File.foreach('file', 'r'){|l| print l}
Hope this helps.
matz.