# What is the right command for file cose

**URL:** <https://www.ruby-forum.com/t/what-is-the-right-command-for-file-cose/117242>\
**Category:** Rails\
**Created:** [October 15, 2007, 8:13am UTC](https://www.ruby-forum.com/t/what-is-the-right-command-for-file-cose/117242 "2007-10-15T08:13:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 15, 2007, 8:13am UTC](https://www.ruby-forum.com/t/what-is-the-right-command-for-file-cose/117242/1 "2007-10-15T08:13:58Z")

</div>

Hi All,

I want to know, which is the right command to close the file, f.close  
is the right command, or any, please let me know

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 15, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/what-is-the-right-command-for-file-cose/117242/2 "2007-10-15T13:59:26Z")

</div>

On 10/15/07, Vidya V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi All,
> 
> I want to know, which is the right command to close the file, f.close  
> is the right command, or any, please let me know

Assuming f is a File, yes f.close will close it.

On the other hand, if possible, it’s better to use the form of  
File.open which takes a block

File.open(file\_path) do | f |  
# code that works with the file referenced by f  
end

This automatically closes the file when the block finishes.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 15, 2007, 2:36pm UTC](https://www.ruby-forum.com/t/what-is-the-right-command-for-file-cose/117242/3 "2007-10-15T14:36:21Z")

</div>

- Read [http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/)
- This question belongs to ruby-lang mailing list

On 10/15/07, Vidya V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi All,
> 
> ## I want to know, which is the right command to close the file, f.close is the right command, or any, please let me know
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Cheers!

- Pratik  
[http://m.onkey.org](http://m.onkey.org)
