0

I'm banging through Zed A. Shaw's learn code the hard way (Ruby) on compilr and am stuck on exercise 16.

filename = ARGV.first
target = File.open(filename, 'w')
target.truncate(target.size)
target.close()

In the console, I type

run sample.txt

This should wipe the sample.txt file, but it doesn't.

The file, sample.txt is in the same folder as the Start file.

Any clues?

OctaviaLo
  • 1,268
  • 2
  • 21
  • 46
  • Do you want to delete the content or delete the file from the location? In which platform you are? means which OS? As doc is saying **Truncates file to at most integer bytes. The file must be opened for writing. *Not available on all platforms*.** – Arup Rakshit Oct 01 '13 at 13:37
  • I'm using this online platform called Compilr (http://compilr.com). I tried it in on my Mac Terminal with a text editor and it works fine. So it probably is something to do with Compilr. I've been searching high and low for why I can't write to the file and can't find and answer. – OctaviaLo Oct 02 '13 at 14:08

1 Answers1

1

Ok it's not the Ruby issue (as I expected) it's to do with how Compilr works. By running the code without having first created a sample.txt file, Compilr created the file for me by default in the content folder. So putting the uh... writable files... into the contents folder enables Compilr to write to them. Putting them all in the same folder (the script and the files) makes it not work.

OctaviaLo
  • 1,268
  • 2
  • 21
  • 46
  • NB: Compilr's text editor won't refresh to show the new file (you have to make it first), and doesn't refresh (you have to close it and reopen it to see the content). – ashes999 Mar 15 '14 at 21:53