# Closing files with abrupt interruptions

**URL:** <https://www.ruby-forum.com/t/closing-files-with-abrupt-interruptions/129212>\
**Category:** Ruby\
**Created:** [February 15, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/closing-files-with-abrupt-interruptions/129212 "2008-02-15T14:40:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [February 15, 2008, 2:40pm UTC](https://www.ruby-forum.com/t/closing-files-with-abrupt-interruptions/129212/1 "2008-02-15T14:40:41Z")

</div>

loop do  
File.open somefile, ‘r’ do |io|  
next  
end  
end

Does somefile get closed?  
How would I test that?  
I have the same question if it raises an error (that gets rescued) or  
throws a symbol.

Daniel Brumbaugh K.

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [February 15, 2008, 2:49pm UTC](https://www.ruby-forum.com/t/closing-files-with-abrupt-interruptions/129212/2 "2008-02-15T14:49:33Z")

</div>

Daniel Brumbaugh K. wrote:

> Daniel Brumbaugh K.

There are two ways to open a file for reading in ruby, the first is:

f = File.open(“fred.txt”, “r”)  
f.each do |l|  
puts l  
end  
f.close

in the above case you have to explicitly close the file, however with  
this:

File.open(“fred.txt”,“r”) do |f|  
f.each do |l|  
puts l  
end  
end

The opened file is referenced by the variable f, however f is in the  
scope of the ‘File.open() do … end’ and once the program goes past the  
closing ‘end’ the f will be removed. The deletion of the file handle  
triggers the close. So no need for an explicit close.

The second way is also more ‘rubyish’.

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [February 15, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/closing-files-with-abrupt-interruptions/129212/3 "2008-02-15T23:10:35Z")

</div>

On 15.02.2008 14:39, Daniel Brumbaugh K. wrote:

> loop do  
> File.open somefile, ‘r’ do |io|  
> next  
> end  
> end
> 
> Does somefile get closed?

Yes. See also Peter’s explanation.

> How would I test that?

Easily:

robert@fussel ~  
$ echo 1 \> x

irb(main):001:0\> io = nil  
=\> nil  
irb(main):002:0\> File.open(“x”) {|io| p io, io.closed?}  
#\<File:x\>  
false  
=\> nil  
irb(main):003:0\> p io, io.closed?  
#\<File:x (closed)\>  
true  
=\> nil

> I have the same question if it raises an error (that gets rescued) or  
> throws a symbol.

irb(main):004:0\> File.open(“x”) {|io| p io, io.closed?; raise “E”}  
#\<File:x\>  
false  
RuntimeError: E  
from (irb):4  
from (irb):4:in `open’  
from (irb):4  
from :0  
irb(main):005:0\> p io, io.closed?  
#\<File:x (closed)\>  
true  
=\> nil

Cheers

robert
