[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Reg IO class

Abirami Selvam

3/23/2009 5:58:00 AM

Hi,
Should we close the file after reading it?

Please see the code below...


data = IO.readlines("test.txt","r")
diskfile = File.open("test.xml","w+")
data.each do |line|
line.chomp!
diskfile.puts(line) if line.length>0
end

It is working fine, even without closing the file(diskfile.close). Can
someone suggest me. Does the file close automatically or not?


Thanks in advance

Abirami
--
Posted via http://www.ruby-....

2 Answers

Robert Klemme

3/23/2009 6:49:00 AM

0

On 23.03.2009 06:58, Abirami Selvam wrote:
> Hi,
> Should we close the file after reading it?

Yes, of course.

> Please see the code below...
>
>
> data = IO.readlines("test.txt","r")
> diskfile = File.open("test.xml","w+")
> data.each do |line|
> line.chomp!
> diskfile.puts(line) if line.length>0
> end
>
> It is working fine, even without closing the file(diskfile.close). Can
> someone suggest me. Does the file close automatically or not?

In the first line yes, in the second chunk, no. This can be easily
fixed by using the block form of File.open.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

Abirami Selvam

3/23/2009 8:35:00 AM

0

Robert Klemme wrote:
> On 23.03.2009 06:58, Abirami Selvam wrote:
>> Hi,
>> Should we close the file after reading it?
>
> Yes, of course.
>
>> It is working fine, even without closing the file(diskfile.close). Can
>> someone suggest me. Does the file close automatically or not?
>
> In the first line yes, in the second chunk, no. This can be easily
> fixed by using the block form of File.open.
>
> Cheers
>
> robert

Thank you for your qucik reply
--
Posted via http://www.ruby-....