# Ruby Class documentation

**URL:** <https://www.ruby-forum.com/t/ruby-class-documentation/136581>\
**Category:** Ruby\
**Created:** [May 3, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/ruby-class-documentation/136581 "2008-05-03T23:14:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 3, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/ruby-class-documentation/136581/1 "2008-05-03T23:14:11Z")

</div>

Hi,

I noticed that when I save an internet resource to my local disk, ruby  
keeps the file open until the program terminates. After some searching I  
noticed that close method should be called after you’re done with the  
writing part.  
I checked the File class documentation at  
[http://ruby-doc.org/core/classes/File.html](http://ruby-doc.org/core/classes/File.html) several times but there’s not  
trace of the close method. I assume that File inherits from another  
class which actually contains the close method. I’ve also searched for  
the parent class in the File documentation but couldn’t find that  
either.

So my question is: Where can I find the documentation on the File.close  
method? If File actually is a subclass of another class then how is this  
indicted in the documentation?

Regards,  
DarnDao

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 3, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/ruby-class-documentation/136581/2 "2008-05-03T23:40:05Z")

</div>

Aa Aa wrote:

> So my question is: Where can I find the documentation on the File.close  
> method? If File actually is a subclass of another class then how is this  
> indicted in the documentation?

File gets close from its parent IO. On the page you linked to, the  
parent of File is shown in the line “Parent: IO” in the upper-left  
corner.

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 3, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/ruby-class-documentation/136581/3 "2008-05-03T23:41:50Z")

</div>

On Sat, May 3, 2008 at 11:14 PM, Aa Aa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the parent class in the File documentation but couldn’t find that  
> either.
> 
> So my question is: Where can I find the documentation on the File.close  
> method? If File actually is a subclass of another class then how is this  
> indicted in the documentation?

In the upper lefthand corner there is Parent: IO, and IO is a  
hyperlink to IO documentation.  
IO contains the close method.

Related to this:

It’s usually better to use block form of File.open, than File.new and  
File#close.  
i.e.  
instead of

f = File.new(‘xxxx’,'‘w’)  
f \<\< “dfsdfsd”  
f.close

do

File.open(‘xxxx’,‘w’) do |f|  
f \<\< “fdfdsfsdf”  
end

The file will be closed when the control exits from the block (even in  
the case of an exception).

File.new is usually used when you need to keep the file open longer  
than a scope of one method.  
(setting a member/attribute, returning open file from a method, etc.)

J.

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 4, 2008, 12:02am UTC](https://www.ruby-forum.com/t/ruby-class-documentation/136581/4 "2008-05-04T00:02:22Z")

</div>

> In the upper lefthand corner there is Parent: IO, and IO is a  
> hyperlink to IO documentation.  
> IO contains the close method.

Thanks, I probably missed that because it doesn’t look like something  
you can click on 🙂

> Related to this:
> 
> It’s usually better to use block form of File.open, than File.new and  
> File#close.  
> i.e.  
> instead of
> 
> f = File.new(‘xxxx’,’‘w’)  
> f \<\< “dfsdfsd”  
> f.close
> 
> do
> 
> File.open(‘xxxx’,‘w’) do |f|  
> f \<\< “fdfdsfsdf”  
> end
> 
> The file will be closed when the control exits from the block (even in  
> the case of an exception).
> 
> File.new is usually used when you need to keep the file open longer  
> than a scope of one method.  
> (setting a member/attribute, returning open file from a method, etc.)
> 
> J.

Really useful tip, looks similar to the C# using statement.

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 5, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/ruby-class-documentation/136581/5 "2008-05-05T14:36:18Z")

</div>

Aa Aa wrote:

> > In the upper lefthand corner there is Parent: IO, and IO is a  
> > hyperlink to IO documentation.  
> > IO contains the close method.
> 
> Thanks, I probably missed that because it doesn’t look like something  
> you can click on 🙂
> 
> > Related to this:
> > 
> > It’s usually better to use block form of File.open, than File.new and  
> > File#close.  
> > i.e.  
> > instead of
> > 
> > f = File.new(‘xxxx’,’‘w’)  
> > f \<\< “dfsdfsd”  
> > f.close
> > 
> > do
> > 
> > File.open(‘xxxx’,‘w’) do |f|  
> > f \<\< “fdfdsfsdf”  
> > end

And can become an oneliner.

File.open(‘xxxx’,‘w’) { |f| f \<\< “fdfdsfsdf” }

by  
TheR
