Forum: Ruby Reg IO class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Aa9698375c67dacb97dafcaeefff7e3e?d=identicon&s=25 Abirami Selvam (abirami)
on 2009-03-23 07:01
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-03-23 07:52
(Received via mailing list)
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
Aa9698375c67dacb97dafcaeefff7e3e?d=identicon&s=25 Abirami Selvam (abirami)
on 2009-03-23 09:37
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
This topic is locked and can not be replied to.