Reg IO class

A

Abirami Selvam

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
 
R

Robert Klemme

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
 
A

Abirami Selvam

Robert said:
Yes, of course.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

IO from text file 5
Random Access using IO#pos in code blocks 13
io module and pdf question 2
IO vs. File 5
Noob File IO question 8
Chatbot 0
Threaded IO trouble 12
Resolving unicode escapes to unicode character 1

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top