# When file gets closed in a block

**URL:** <https://www.ruby-forum.com/t/when-file-gets-closed-in-a-block/107216>\
**Category:** Ruby\
**Created:** [June 19, 2007, 11:11am UTC](https://www.ruby-forum.com/t/when-file-gets-closed-in-a-block/107216 "2007-06-19T11:11:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 19, 2007, 11:11am UTC](https://www.ruby-forum.com/t/when-file-gets-closed-in-a-block/107216/1 "2007-06-19T11:11:09Z")

</div>

I like to use this a lot:  
File.new(cName,‘wb+’).write(‘aText’)

I have understood that file gets automaticly closed. But when? You can  
not delete file right after above statement. If you want to delete file  
you must use this.  
f = File.new(cName,‘wb+’)  
f.write(attachment.unpack(‘m’))  
f.close

When does the file in block gets closed.

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 19, 2007, 11:24am UTC](https://www.ruby-forum.com/t/when-file-gets-closed-in-a-block/107216/2 "2007-06-19T11:24:58Z")

</div>

If you open a file with File#new, you have to close it yourself by  
calling File#close.

* * *

## f = File.new(‘someFilename’, ‘w’) f.write(text1) f.write(text2) f.close

You can write as often as you like to f as long as you don’t close it.

File#open closes the file automatically if passed a block:

* * *

## File.open(‘someFilename’, ‘w’) {|f| f.write(text1) f.write(text2) }

Without a block File#open ist just an alias for File#new.

Sascha

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 19, 2007, 11:52am UTC](https://www.ruby-forum.com/t/when-file-gets-closed-in-a-block/107216/3 "2007-06-19T11:52:11Z")

</div>

> You can write as often as you like to f as long as you don’t close it.
> 
> File#open closes the file automatically if passed a block:
> 
> * * *
> 
> File.open(‘someFilename’, ‘w’) {|f|  
> f.write(text1)  
> f.write(text2)  
> }

I guess then my one liner should look like this:

File.new(cName,‘wb+’) { |f|.write(‘aText’) }

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [June 19, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/when-file-gets-closed-in-a-block/107216/4 "2007-06-19T16:10:53Z")

</div>

On Jun 19, 2007, at 5:52 AM, Damjan R. wrote:

> I guess then my one liner should look like this:
> 
> File.new(cName,‘wb+’) { |f|.write(‘aText’) }
> 
> by  
> TheR

Close, try it like this:

File.open(cName,‘wb+’) { |f| f.write(‘aText’) }

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
