File I/O

N

name pipe

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 ?
 
P

Peter Szinek

name said:
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
 
J

Jeremy McAnally

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
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top