Civatrix Civatrix - 1 year ago 121
Ruby Question

Create file in Ruby

I'm pretty new to Ruby and something has me entirely confused. I'm trying to create a new file and things don't seem to be working as I expect them too. Here's what I've tried: "out.txt" "out.txt" "out.txt","w" "out.txt","w"

According to everything I've read online all of those should work but every single one of them gives me this:

ERRNO::ENOENT: No such file or directory - out.txt

This happens from irb as well as a ruby file. What am I missing?


Answer Source"out.txt", [your-option-string]) {|f| f.write("write your stuff here") }

where your options are:

r - Read only. The file must exist.
w - Create an empty file for writing.
a - Append to a file.The file is created if it does not exist.
r+ - Open a file for update both reading and writing. The file must exist.
w+ - Create an empty file for both reading and writing.
a+ - Open a file for reading and appending. The file is created if it does not exist.

In your case, w is preferable.

OR you could have:

out_file ="out.txt", "w")
out_file.puts("write your stuff here")

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download