# File I/O

**URL:** <https://www.ruby-forum.com/t/file-i-o/74654>\
**Category:** Ruby\
**Created:** [November 7, 2006, 10:51am UTC](https://www.ruby-forum.com/t/file-i-o/74654 "2006-11-07T10:51:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![name\_pipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b5a626/32.png) [@name\_pipe](https://www.ruby-forum.com/u/name_pipe)\
**Post date:** [November 7, 2006, 10:51am UTC](https://www.ruby-forum.com/t/file-i-o/74654/1 "2006-11-07T10:51:39Z")

</div>

Hello,

I have some text to be written to a file.  
After receiving data from webrick server I modify it lil bit and then  
write it to a file.

fd = File.open(“file.txt”, “w”)  
fd.puts “#{data}”

The code doesnt give error and runs successfully however file.txt  
doesnt contain any data.  
Just puts “#{data}” gives a give output on console. Means its not empty.

Can anyone tell me why does it happen ?

---

<div class="post-metadata">

**Author:** ![name\_pipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b5a626/32.png) [@name\_pipe](https://www.ruby-forum.com/u/name_pipe)\
**Post date:** [November 7, 2006, 10:51am UTC](https://www.ruby-forum.com/t/file-i-o/74654/2 "2006-11-07T10:51:40Z")

</div>

name pipe wrote:

> Hello,
> 
> I have some text to be written to a file.  
> After receiving data from webrick server I modify it lil bit and then  
> write it to a file.
> 
> fd = File.open(“file.txt”, “w”)  
> You can use

open(“file.txt”, “w”)

as well.

> fd.puts “#{data}”  
> Why not just

fd.puts data

as for the empty file problem, maybe try to close fd:

fd.close

Peter  
[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![name\_pipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b5a626/32.png) [@name\_pipe](https://www.ruby-forum.com/u/name_pipe)\
**Post date:** [November 7, 2006, 10:51am UTC](https://www.ruby-forum.com/t/file-i-o/74654/3 "2006-11-07T10:51:42Z")

</div>

Hello,  
That works, or, even more Rubytastic:

open(“file.txt”, “w”) do |fd|  
fd.puts data  
end

Using the block automatically closes the file. 🙂

–Jeremy

---

<div class="post-metadata">

**Author:** ![name\_pipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b5a626/32.png) [@name\_pipe](https://www.ruby-forum.com/u/name_pipe)\
**Post date:** [November 7, 2006, 10:51am UTC](https://www.ruby-forum.com/t/file-i-o/74654/4 "2006-11-07T10:51:43Z")

</div>

On Wed, 01 Nov 2006 13:50:49 -0000, name pipe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> doesnt contain any data.  
> Just puts “#{data}” gives a give output on console. Means its not empty.
> 
> Can anyone tell me why does it happen ?

You’re never closing the file, which is why it’s empty (your writes are  
never flushed).  
Try either fd.close at the end, or (better IMHO) use the block form:

File.open(“file.txt”, “w”) do |fd|  
fd.puts “#{data}” # or fd.puts data.to\_s  
end

Hope that helps.
